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

조회 수: 3 (최근 30일)
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!

채택된 답변

Ingrid
Ingrid 2016년 1월 7일
편집: Ingrid 2016년 1월 7일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by