getappdata and setappdata (or get and set)

4 views (last 30 days)
HRmatlab
HRmatlab on 7 May 2015
Commented: HRmatlab on 7 May 2015
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.

Answers (1)

Geoff Hayes
Geoff Hayes on 7 May 2015
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 Comments
Geoff Hayes
Geoff Hayes on 7 May 2015
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 on 7 May 2015
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.

Sign in to comment.

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!