How to retrieve the output from callback function?

84 visualizzazioni (ultimi 30 giorni)
I have designed a pushbutton on a MATLAB figure and written a callback function, while the function runs without any error, it does not produce any output. May I know how to retrieve the output from the callback function. Here is my code :
The screen :
f=figure('Position',[10 100 1500 700]);
pb1 = uicontrol(f,'Style','pushbutton','String','Slice# in yz plane',...
'Position',[1425 600 100 50],...
'callback',@(src,eventdata)pb1_callback(slice_numx));
The callback function :
function[slice_num] = pb1_callback(src,eventdata)
prompt={'Enter the Slice Number'};
slicenum = inputdlg(prompt);
slice_num = str2double(slicenum);
When I press the pusbutton 'pb1', it gives a popup window wherein I input the number. Issue is that I am not able to find this entered number in my main program. I am sure there is some issue with defining it in main program. Any help will be upvoted.
Cheers,
Guru
  2 Commenti
jupiter
jupiter il 19 Dic 2016
Adding a question based on the replies, is it possible to pass an array to callback function? i am trying the code below, but the array elements are not getting passed into the callback function.
pb1 = uicontrol(f,'Style','pushbutton','String','Slice# in yz plane',...
'Position',[1425 600 100 50],...
'callback',@(src,eventdata,samplemag)pb1_callback);
And the callback function is :
function pb1_callback(src,eventdata,samplemag)
But the data from 'samplemag' is not getting passed into the function. Any help?
Adam
Adam il 19 Dic 2016
What is samplemag?
@(src,eventdata,samplemag)pb1_callback
tells the code that when pb1_Callback is called it expects the caller to give it those 3 arguments, all of which will then be ignored.
@(src,eventdata)pb1_callback( samplemag )
with a function definition of
function pb1_callback(samplemag)
is likely what you want, unless you also want src or eventdata in which case pass them in the final brackets of the callback definition too.
In terms of the original question you can achieve the equivalent of this either by using nested functions or by passing an object of a handle-derived class into your callback.

Accedi per commentare.

Risposta accettata

Jan
Jan il 16 Dic 2016
Callbacks do not have any output. They react to a user action, so where should the put any output to? There is even no "main" program, when a GUI is active. This is not the purpose of a GUI.
You display Outputs in the CommandWindow or the GUI or store it for later use inside the GUI. The later is done either by guidata, set/getappdata or by writing to the UserData of an object or the GUI. You can store data as persistent variables in another function also.
If your "main" program waits, until the GUI is finished, your GUI can output some values or you can request them by guidata before the window is deleted. The best way depends on where you need which data and if your GUI is created by GUIDE or programmatically.
Search in this forum for "GUI share data callbacks"
  2 Commenti
jupiter
jupiter il 16 Dic 2016
Modificato: jupiter il 16 Dic 2016
Ok, Can we pass an array as an input to the callback function? My requirement is that when I click the push button and enter the slice number, it should plot the graph for that slice. If I can get this array into the function input, I can write the plot command inside the callback function. I tried giving it as follows.(samplemag is the array in question)
But it does not pass the array values into the callback function.
f=figure('Position',[10 100 1500 700]);
pb1 = uicontrol(f,'Style','pushbutton','String','Slice# in yz plane',...
'Position',[1425 600 100 50],...
'callback',@(src,eventdata,samplemag)pb1_callback);
And the callback function is :
function pb1_callback(src,eventdata,samplemag)
Jan
Jan il 19 Dic 2016
Use:
'callback', {@pb1_callback, samplemag})

Accedi per commentare.

Più risposte (2)

Henry Giddens
Henry Giddens il 16 Dic 2016
Hi,
You will have to store the answer as a property or else if is will be lost when the callback function finishes executing. There are a number of ways to do this. You can store it in the figures UserData Property:
%At the end of the callback function:
src.Parent.UserData = slice_num;
%When you want to retrieve it from somewhere else:
slice_num = f.UserData;
You can also use setappdata and getappdata, to pass data between various callback functions.

Lavi Sekundo
Lavi Sekundo il 20 Feb 2018
I just realized this one (e.g.)
fig.CheckBox.ValueChangedFcn='outVar=myFunction(inVar);';
when you execute the callback by selecting/un-selecting a CheckBox in the GUI, you are in fact executing whatever is in the quotes. Just define your function as you would do anyway
function outVar=myFunction(inVar)
outVar=inVar
  2 Commenti
Stephen23
Stephen23 il 20 Feb 2018
The MATLAB documentation states "Defining a callback as a character vector is not recommended", and recommends using function handles.
Summary: Avoid using a char vector, always use a function handle: they are more flexible, and much more reliable.
Jan
Jan il 20 Feb 2018
@Lavi: This is a bad idea. The string is evaluated in the base workspace and this can have all evil side-effects as other eval calls. E.g. redefine "myFunction" as a variable in the command window and afterwards your callback crash. It will be very hard to debug such remote effects.

Accedi per commentare.

Categorie

Scopri di più su Interactive Control and Callbacks in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by