필터 지우기
필터 지우기

getappdata and setappdata (or get and set)

조회 수: 1 (최근 30일)
HRmatlab
HRmatlab 2015년 5월 7일
댓글: HRmatlab 2015년 5월 7일
Disclaimer: I am new Matlab GUI programming so this may be a truly stupid question :)
I am trying to retrieve data from a GUI callback, i.e. press a button, call a function, perform an operation and then retrieve the result of that operation.
I will demonstrate with this very simple example code:
fh = figure();
btnHandle = uicontrol(fh,'style','pushbutton','UserData',[],'Callback',@fun1_callback);
myValues = getappdata(btnHandle, 'UserData');
display(myValues);
function out = fun1_callback(hobj,evt)
out = rand(5);
setappdata ( hobj , 'UserData' , out );
end
Although the callback function (fun1_callback) correctly allocates the UserData to the variable (out) the main program gets zeros for the return values (myValues), unless I insert a pause statement like this:
fh = figure();
btnHandle = uicontrol(fh,'style','pushbutton','UserData',[],'Callback',@fun1_callback);
pause (5.0);
myValues = getappdata(btnHandle, 'UserData');
display(myValues);
function out = fun1_callback(hobj,evt)
out = rand(5);
setappdata ( hobj , 'UserData' , out );
end
I am confused why that is. Any help would be greatly appreciated.

답변 (1개)

Geoff Hayes
Geoff Hayes 2015년 5월 7일
HRmatlab - the reason that it works is because you must be pressing the push button before the five second pause has elapsed (and so the callback fires and the UserData is initialized. Note that the lines of code are evaluated sequentially (without any pause or wait between each one)
% create the figure
fh = figure();
% create the push button
btnHandle = uicontrol(fh,'style','pushbutton','UserData',[],'Callback',@fun1_callback);
% get the data from the btnHandle
myValues = getappdata(btnHandle, 'UserData');
% display the values
display(myValues);
So each line follows the other and unless you somehow press the button between the lines where the btnHandle and the myValues are initialized then the latter will be all zeros (or empty as it is in my case).
How or where in your code do you expect to use the values that are generated by the push button?
  댓글 수: 3
Geoff Hayes
Geoff Hayes 2015년 5월 7일
You're assumption is correct - the fun1_callback is only called when you press the pushbutton. The problem is that you are pressing the button too late which causes this function to be evaluated after you have passed the line in your code where you call getappdata.
If you have other buttons or controls (edit text fields) etc. with one button to plot everything, then you can continue as above but just remember that your plot button (and so its callback) would be responsible for gathering all of the information to do the calibration etc. So it would call getappdata against each control that you had previously saved data to.
You may want to consider using GUIDE to build your GUI. See creating a GUI with GUIDE for details.
HRmatlab
HRmatlab 2015년 5월 7일
Thanks. I think I understand now how it works. I was trying to avoid GUIDE but I think that may be the best option. I really appreciate the feedback.

댓글을 달려면 로그인하십시오.

카테고리

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