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

조회 수: 104 (최근 30일)
BALAGURU S
BALAGURU S 2016년 9월 25일
댓글: Áron Molnár 2022년 8월 4일
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