How to Return value from Callback function to the main function?
105 ビュー (過去 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.
0 件のコメント
採用された回答
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
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
2019 年 4 月 24 日
It's bad practice but you can use global variables to communicate between the functions.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Migrate GUIDE Apps についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!