MATLAB Answers

getting 2 outputs in a GUI function, then using the outputs in main script

7 views (last 30 days)
Tanner
Tanner on 7 Jan 2016
Commented: Tanner on 7 Jan 2016
Hi,
I'm working on a GUI that will have the user type in a number and select A or B from a popup menu, then click a pushbutton to output the values and use them in the main script. Here is what I have so far:
function [out1,out2]=menu
f = figure('Position',[350 350 500 300]);
x = uicontrol('Style', 'edit',...
'String', {''},...
'Position', [100 250 100 50]);
y = uicontrol('Style', 'popup',...
'String', {'A','B'},...
'Position', [100 170 100 50]);
z = uicontrol('Style', 'pushbutton',...
'String', {'OK'},...
'Position', [100 100 100 50],...
'Callback', 'uiresume(gcbf)')
uiwait(f)
out1=get(x,'String')
out2=get(y,'Value')
close(f)
end
There are 2 things I need help with:
1. Currently this returns out1 and out2 in the command window, but the function output seems to only save "ans" in my workspace which is a 1x1 cell. How can I get it to return a 1x2 or 2x1 with both my out1 and out2 values?
2. How can I use the values in the main script? I think if I can get the ans matrix to to return both values then I can assign ans{1} and ans{2} in my script. But if I'm trying to use out1 and out2 they don't save in my script's workspace, for which I've tried toying with assignin to no avail.
Thanks for your time!

  0 Comments

Sign in to comment.

Accepted Answer

Ingrid
Ingrid on 7 Jan 2016
Edited: Ingrid on 7 Jan 2016
your function works well if you make the following adjustments
function [out1,out2]=menu
f = figure('Position',[350 350 500 300]);
x = uicontrol('Style', 'edit',...
'String', {''},...
'Position', [100 250 100 50]);
y = uicontrol('Style', 'popup',...
'String', {'A','B'},...
'Position', [100 170 100 50]);
z = uicontrol('Style', 'pushbutton',...
'String', {'OK'},...
'Position', [100 100 100 50],...
'Callback', 'uiresume(gcbf)');
uiwait(f)
out1_str=get(x,'String');
out1 = str2double(out1_str{1});
out2_n=get(y,'Value');
out2_string = get(y,'String');
out2 = out2_string{out2_n};
close(f)
end
and call it with
[out1, out2] = menu;
(instead of menu; which you currently use and gives you the "ans" variable) you can use these variables in your script directly, without the need to use assignin.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!