How to Return value from Callback function to the main function?

104 次查看(过去 30 天)
function PID_Calc
f=figure('position',[10,10,1000,1000]);
hget1 = uicontrol(...
'style','pushbutton',....
'string','G(s)',.....
'position',[200,500,100,50],....
'callback',{@getgsbutton_callback});
f.visible='on';
char x;
uiwait();
display(y);
end
function x = getgsbutton_callback(source,eventdata)
answer = inputdlg('What is the Numerator equation');
display(answer);
x = str2double(answer{:});
display(x);
%display(y);
return
end
This is a code i have written to get value from a user using GUI. Please guide me on how to get the user input from the call back function to the main function.

采纳的回答

Walter Roberson
Walter Roberson 2016-9-26
It is not possible for a uicontrol callback to return a value. See though http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
  1 个评论
Áron Molnár
Áron Molnár 2022-8-4
But as a workaround, - I'm not sure how elegant and fault tolerant this solution is - you can use a graphical object's UserData.
In your example: The callback function has a default input, the source. In your case this is an uibutton, which has a UserData property. This can be any MATLAB data type or format (like even class object too).
Let's se in case of your program:
function PID_Calc
f=figure('position',[10,10,1000,1000]);
hget1 = uicontrol(...
'style','pushbutton',....
'string','G(s)',.....
'position',[200,500,100,50],....
'callback',{@getgsbutton_callback});
f.Visible='on';
%Wait until you set the x
waitfor(~isempty(hget1.UserData))
%Evaluate the "return value" - REMEMBER! This is evaluated just once, as
%the program runs, so even if you change the x in the G(s) later, the
%returnValue variable will not evaluated again
returnValue=hget1.UserData;
%This is miscal. It is here just tor prove, we have the "return value"
show=uicontrol('style','pushbutton','String',...
"Show return value","Position",[400 500 100 50],...
'callback',{@show,returnValue});%Hence it is not evaluated again,
%the first x will be passed always to it
end
function getgsbutton_callback(source,eventdata)
answer = inputdlg('What is the Numerator equation');
display(answer);
x = str2double(answer{:});
%Use the source object's user data
source.UserData=x;
return;
end
function show(src,event,returnValue)
disp(returnValue)
end
But let's say you want to be able to display the "return value" every time. You can do that if you change the show function:
function show(src,event,returnValue)
%The returnValue is not needed here, but if you remove, remove from
%the {@show,returnValue} part too
disp(src.Parent.Children(2).UserData);
end
In this second method, you can reach from the show callback to the first button's user data and use that.

请先登录,再进行评论。

更多回答(1 个)

Benjamin Blacklock
Benjamin Blacklock 2019-4-24
It's bad practice but you can use global variables to communicate between the functions.

类别

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