saving a variable from GUI

10 次查看(过去 30 天)
Katarzyna Dragun
Katarzyna Dragun 2019-6-27
回答: Stephen23 2019-7-2
Hello!
I wrote a simple GUI for testing but I cannot save my variable. This function is used in another script. What I need to do is to save variables val1, val2, val3 and val4 as a vector (loudness = [val1, val2, val3, val4]. I tried many many things but it cannot create the vector as these values are not known when the script is run. The code is available below. I would appreciate your help!
function mytemps
f = figure;
c = uicontrol(f,'Style','popupmenu');
c.Position = [20 150 90 50];
c.String = {'0','1','2', '3', '4', '5', '6', '7', '8', '9', '10'};
c.Callback = @selection1;
d = uicontrol(f,'Style','popupmenu');
d.Position = [160 150 90 50];
d.String = {'0','1','2', '3', '4', '5', '6', '7', '8', '9', '10'};
d.Callback = @selection2;
e = uicontrol(f,'Style','popupmenu');
e.Position = [300 150 90 50];
e.String = {'0','1','2', '3', '4', '5', '6', '7', '8', '9', '10'};
e.Callback = @selection3;
g = uicontrol(f,'Style','popupmenu');
g.Position = [440 150 90 50];
g.String = {'0','1','2', '3', '4', '5', '6', '7', '8', '9', '10'};
g.Callback = @selection4;
function selection1(src,event)
val1 = c.Value;
str = c.String;
val1 = str{val1};
disp(['Loudness noise 1: ' val1]);
end
function selection2(src,event)
val2 = d.Value;
str = d.String;
val2 = str{val2};
disp(['Loudness noise 2: ' val2]);
end
function selection3(src,event)
val3 = e.Value;
str = e.String;
val3 = str{val3};
disp(['Loudness noise 3: ' val3]);
end
function selection4(src,event)
val4 = g.Value;
str = g.String;
val4 = str{val4};
disp(['Loudness noise 4: ' val4]);
end
end
  2 个评论
Stephen23
Stephen23 2019-6-27
编辑:Stephen23 2019-6-27
Using numbered variables is a sign that you are doing something wrong. Instead you should use one variable and indexing.
Rather than copy-and-pasting code, just define one callback function where the third input agument is that index:
Katarzyna Dragun
Katarzyna Dragun 2019-7-2
Hi Stephen,
thanks! However, even if I remove numbering and just keep one function, the result does not save in the variables space.

请先登录,再进行评论。

回答(1 个)

Stephen23
Stephen23 2019-7-2
You forgot to actually pass those variables back to the main workspace:
I recommend using nested worskpaces, which makes code simple and intuitive

类别

Help CenterFile Exchange 中查找有关 Discrete Multiresolution Analysis 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by