Output of callback function

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

Stephen23
Stephen23 on 13 Mar 2019
Edited: Stephen23 on 13 Mar 2019
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.
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.
Hi Stephen,
thanks for your anwser! I am using GUIDE. The figure with the axes and the button will be oppened when I click on a pushbutton on my GUI. I couldn't apply it on my case.
For the button I used this. The select_area function calculate the new_matrix (output of the funtion).
set(button,'Callback',@(hObject,eventdata) select_area(ax1,values_matrix,ax2) );
On the figure I have a slider to smooth the data of the plot on ax2. That means I need the new_matrix to use it in the funtion (slider_function), which the slider calls in its callback.
set(slider,'Callback',@(hObject,eventdata) slider_function(ax2,new_matrix,get(slider,'Value')) )
How can I get the new_matrix from select_area and use it in slider_function?
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.
Hi Guillaume,
hier is the callback for the pushbutton which oppens the figure:
% --- Executes on button press in pushbutton_3D_Interpretation.
function pushbutton_3D_Interpretation_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_3D_Interpretation (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
values_matrix = getappdata(Pressure_Evaluation, 'matrix');
values_cropped_matrix = getappdata(Pressure_Evaluation, 'croped_matrix');
vector = values_matrix(:,20);
image = getimage(handles.axes_display_image);
set(0,'defaultfigureposition',[50 40 1000 700]);
fig = figure
pos1 = [0.01 0.2 0.4 0.6];
ax1 = subplot('Position',pos1)
imshow(image);
title('image')
pos2 = [0.5 0.15 0.4 0.7];
ax2 = subplot('Position',pos2)
surf(values_matrix);
button = uicontrol('Style', 'push', 'String', 'Next','Position',...
[160 5 100 35],'Units','normalized', 'String', 'select area');
set(button,'Callback',@(hObject,eventdata) select_area(ax1,values_matrix,ax2) );
slider = uicontrol('Style','slider','Min',0,'Max',1,'Value',1,...
'BackgroundColor','white','Position',[570 5 200 20],...
'Units','normalized');
set(slider,'Callback',@(hObject,eventdata) slider_function(ax2,get(slider,'Value')) )
I know that I should use setappdata and getappdata. But I don't know now where to use them. I just wnat to get the matrix from select_area and use it in slider_function. It's the first time that I use a callback in a callback that's why I coudn't fix it.
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?
I want to have only one figure with the to axes.
Guillaume
Guillaume on 13 Mar 2019
Edited: Guillaume on 13 Mar 2019
So what should happen if the user press on pushbutton_3D_Interpretation a second time?
You have right, it will open a second figure. And I loose the matrix I used in the first figure because it will be updated. It's beeing very complecated. Let's assume that the first figure will be closed if the user press the button a second time. How should I do it?

Sign in to comment.

Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Tags

Asked:

on 13 Mar 2019

Commented:

on 13 Mar 2019

Community Treasure Hunt

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

Start Hunting!