saving a variable from GUI

조회 수: 13 (최근 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에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by