Output of callback function
Show older comments
Hi,
on a figure I have 2 axes and a pushbutton. For the pushbutton I used this code to define its callback:
set(button,'Callback',@(hObject,eventdata) button_function(ax1,matrix,ax2) );
The button_function allows to select a part of the input matrix on ax1 and plot it on ax2. And the output of this function is the new matrix (the selected part of the input matrix). To save this new matrix I used this:
new_matrix = set(button,'Callback',@(hObject,eventdata) button_function(ax1,matrix,ax2) );
But the value of new_matrix is [ ] . How can I get it right?
Thanks for your help.
9 Comments
GUI callback functions do not have any output arguments:
The output of set is not the output of any function (read the set documentation to know what its output is). Nor does it even make sense that asynchronous code (like that of a GUI callback) would return a value immediately when you set the callback function of some GUI object.
The documentation explains how to pass data between GUI workspaces:
If you are commendably writing your own GUI then I recommend using nested functions: these are intuitive and easy to debug. If you are unfortunately using GUIDE then use guidata.
Guillaume
on 13 Mar 2019
There's always the option of using the App designer which has a better design than GUIDE, as long as it has the features you need. It's probably less daunting than writing your own GUI from scratch.
Ouael Chkoundali
on 13 Mar 2019
Edited: Ouael Chkoundali
on 13 Mar 2019
Guillaume
on 13 Mar 2019
I couldn't apply it on my case.
What couldn't you apply? There's no reason that the standard way of passing data between callbacks in GUIDE wouldn't work for you.
Note that GUIDE can automatically create the callback call for you (which will include the necessary machinery to pass the handle structure to your callback, iirc). Your callbacks discard the hObject which is usually useful.
I'm also doubtful that you're actually passing the correct data to your callback. Note that the values of ax1, values_matrix, etc. that your callback will receive will always be the values they were when you created the callback, even if you change them afterwards. Basically, they'll be constants as far as the callback is concerned.
Ouael Chkoundali
on 13 Mar 2019
Guillaume
on 13 Mar 2019
Ok, now I think I understand what you're doing.
First, I would recommend that you explicitly pass the parent to the axes, and control creation function rather than letting matlab use the current figure. At the moment, it shouldn't create any problem but should matlab ever becomes multithreaded the current figure may not be the one you've just created.
ax1 = subplot('Parent', fig, 'Position', pos1); %guaranteed to be in fig
button = uicontrol(fig, 'Style', ...) %guaranted to be in fig
Can there be multiple figures opened at the same time, each with their own slider (meaning that the matrix to pass between callbacks vary from figure to figure)? Are you currently using the UserData property of the figures?
Ouael Chkoundali
on 13 Mar 2019
Ouael Chkoundali
on 13 Mar 2019
Answers (0)
Categories
Find more on Interactive Control and Callbacks 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!