getappdata and setappdata (or get and set)

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.

Antworten (1)

Geoff Hayes
Geoff Hayes am 7 Mai 2015

0 Stimmen

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 Kommentare

HRmatlab
HRmatlab am 7 Mai 2015
Thank you Geoff. You are correct of course, that's exactly the case. But I guess I am still confused about how uicontrol works. I was under the assumption that the Callback (@fun1_callback) is only called when I pressed the pushbutton. I am clearly wrong. In any case all I am trying to do is have several buttons to read data, apply calibrations/corrections to the data, then do a fit and display the results and the raw data on various plots. I am doing that from the command line right now. I just thought a simple GUI may be a little easier.
Geoff Hayes
Geoff Hayes am 7 Mai 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 am 7 Mai 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 7 Mai 2015

Kommentiert:

am 7 Mai 2015

Community Treasure Hunt

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

Start Hunting!

Translated by