MATLAB: Getting 2 outputs in a GUI function, then using the outputs in main script

callbackfunctiongui

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 1×1 cell. How can I get it to return a 1×2 or 2×1 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!

Best Answer

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.
Related Question