main function of GUI with a sub function, I want to get the output of sub function as input
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponse acceptée
Walter Roberson
le 25 Juin 2017
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 commentaires
Walter Roberson
le 25 Juin 2017
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.
Plus de réponses (1)
Image Analyst
le 25 Juin 2017
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 commentaires
Walter Roberson
le 25 Juin 2017
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.
Voir également
Catégories
En savoir plus sur Language Support dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!