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

105 views (last 30 days)
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.

Accepted Answer

Walter Roberson
Walter Roberson on 26 Sep 2016
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 Comment
Áron Molnár
Áron Molnár on 4 Aug 2022
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.

Sign in to comment.

More Answers (1)

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

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!