saving a variable from GUI

13 Ansichten (letzte 30 Tage)
Katarzyna Dragun
Katarzyna Dragun am 27 Jun. 2019
Beantwortet: Stephen23 am 2 Jul. 2019
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 Kommentare
Stephen23
Stephen23 am 27 Jun. 2019
Bearbeitet: Stephen23 am 27 Jun. 2019
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 am 2 Jul. 2019
Hi Stephen,
thanks! However, even if I remove numbering and just keep one function, the result does not save in the variables space.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

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

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by