main function of GUI with a sub function, I want to get the output of sub function as input

2 次查看(过去 30 天)
In my gui function there is a sub function (fcn1) that I want to get its output (y) for a push button callback (fcn2) in the main function, but I cannot get the output of sub function: I appreciate if anyone can help;
function [V]=Problem
sz=get(0,'ScreenSize');
figure('numbertitle','off','menubar','none','position',[sz(3)/3 sz(4)/4 sz(3)/2.8 sz(4)/1.5]);
uicontrol('style','text','string','Select equation','position',
[20 450 180 35],'fontsize',12,'backgroundcolor','g','horizontalalignment','left');
V.al=uicontrol('style','popup','string',{'choice A','Choice B'},...
'position',[220 450 150 35],'fontsize',12,'backgroundcolor','w','horizontalalignment','left');
set(V.al,'callback',{@fcn1,V});
function [V]=fcn1(varargin)
V=varargin{3};
if get(V.al,'value')==1
helpdlg('3/x+2*x^2+1','Ref. Eqaution');
else
y=inputdlg('Vs=','User Equation',[1,33],{'sind(x)*x^2+1'});
end
end
uicontrol('style','pushbutton','string','ok','backgroundcolor','y','position',[200 20 30 30],'callback','[r]=fcn2(y);close');
end

采纳的回答

Walter Roberson
Walter Roberson 2017-6-25
function [V] = Problem
sz = get(0, 'ScreenSize');
fig = figure('numbertitle', 'off', 'menubar', 'none', 'position', [sz(3)/3 sz(4)/4 sz(3)/2.8 sz(4)/1.5], 'tag', 'Problem_fig');
uicontrol('Parent', fig, 'style', 'text', 'string', 'Select equation', 'position',
[20 450 180 35], 'fontsize', 12, 'backgroundcolor', 'g', 'horizontalalignment', 'left');
V.al = uicontrol('Parent', fig, 'style', 'popup', 'string', {'choice A','Choice B'}, ...
'position', [220 450 150 35], 'fontsize', 12, 'backgroundcolor', 'w', 'horizontalalignment', 'left', ...
'tag', 'V_a1');
set(V.al, 'callback', {@fcn1, V}, 'UserData', '');
uic3 = uicontrol('Parent', fig, 'style', 'pushbutton', 'string', 'ok', 'backgroundcolor', 'y', 'position', [200 20 30 30], 'callback', 'V_a1 = findobj(0, 'tag', ''V_a1''); y = get(V_a1, ''UserData''); [r] = fcn2(y); close(ancestor(V_a1, ''figure''))', 'enable', 'off');
function fcn1(varargin)
V = varargin{3};
if get(V.al, 'value') ==1
helpdlg('3/x+2*x^2+1','Ref. Equation');
else
y = inputdlg('Vs =', 'User Equation', [1,33], {'sind(x)*x^2+1'});
set(V.a1, 'UserData', y);
set(uic3, 'enable', 'on')
end
end
end
  3 个评论
Walter Roberson
Walter Roberson 2017-6-25
It would be better to make the code of the uic3 callback into a separate function, though. Programming callbacks as strings is ugly and error prone.

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2017-6-25
fcn needs to return the variable, y. Otherwise, y is just a local variable used internal to the function and vanishes once the function has finished running.
function [y, V] = fcn1(varargin)
If you need y in functions other than the function that called fcn1(), then see the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
  2 个评论
Walter Roberson
Walter Roberson 2017-6-25
Note that you have
set(V.al,'callback',{@fcn1,V});
When you set something as a callback, it is evaluated at the base workspace, in an environment that expects no outputs, like
feval(varargin{1}, hObject, event, varargin{2:end});
Therefore it cannot usefully return any value. Returning y will not help here. You have to push the y value out to a place that the other function knows to look.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by