Getting the output from a callback in main function

I have the following simple problem. I have created a gui and I would like to get the output of the callback function in main function. A simplified version of my gui is this:
function aGui
a=0;
uicontrol( ...
'Style' , 'Slider' , ...
'max' , 10 , ...
'min' , 0 , ...
'value' , 0 , ...
'sliderstep', [1/10 1/10] , ...
'units' , 'norm' , ...
'position' , [0.11 0.13 0.5 0.05] , ...
'Callback' , {@clbk, a});
end
function clbk(varargin)
sliderH=varargin{1};
a=varargin{3};
b=a-get(sliderH, 'value');
set(sliderH, 'userdata', b);
out=get(sliderH, 'userdata');
end
So when I call out=aGui the output argument out will be the output from the callback. I would ptefer not to use assignin.

Answers (2)

You have to define your function such that it returns the value:
function aGui
a=0;
hSlider = uicontrol( ...
'Style' , 'Slider' , ...
'max' , 10 , ...
'min' , 0 , ...
'value' , 0 , ...
'sliderstep', [1/10 1/10] , ...
'units' , 'norm' , ...
'position' , [0.11 0.13 0.5 0.05] , ...
'Callback' , {@clbk, a});
end
% Now call the function clbk():
out = clkbk(hSlider); % "theResult" will get stuffed into "out".
function theResult = clbk(varargin)
% It's code goes here...

5 Comments

But if I do this:
function out=aGui
a=0;
hSlider = uicontrol( ...
'Style' , 'Slider' , ...
'max' , 10 , ...
'min' , 0 , ...
'value' , 0 , ...
'sliderstep', [1/10 1/10] , ...
'units' , 'norm' , ...
'position' , [0.11 0.13 0.5 0.05] , ...
'Callback' , {@clbk, a});
% Now call the function clbk():
out = clbk(hSlider, a); % "theResult" will get stuffed into "out".
end
function theResult = clbk(varargin)
% It's code goes here...
% function res=clbk(varargin)
%
sliderH=varargin{1};
a=varargin{2};
b=a-get(sliderH, 'value');
set(sliderH, 'userdata', b);
theResult=get(sliderH, 'userdata');
end
out does not change when the slider changes value. This is what I want to achieve. I hope it is clear.
Try it like this:
function out= aGui()
clc;
a=0;
hSlider = uicontrol( ...
'Style' , 'Slider' , ...
'max' , 10 , ...
'min' , 0 , ...
'value' , 0 , ...
'sliderstep', [1/10 1/10] , ...
'units' , 'norm' , ...
'position' , [0.11 0.13 0.5 0.05] , ...
'Callback' , {@clbk, a});
% Now call the function clbk():
out = clbk(hSlider, a); % "theResult" will get stuffed into "out".
end
function theResult = clbk(varargin)
% It's code goes here...
% function res=clbk(varargin)
%
hSlider = varargin{1}
a = varargin{3}
b = a - get(hSlider, 'value')
theResult = b % How I'd do it.
% How you want to do it for some reason:
set(hSlider, 'userdata', b);
theResult=get(hSlider, 'userdata')
end
If you're going to build GUI's the tediouis way by specifying each and every control, including it's position and values and everything else, instead of using GUIDE, then you'd better study all of Matt Fig's 41 examples of how to build a GUI manually. http://www.mathworks.com/matlabcentral/fileexchange/24861-41-complete-gui-examples Or this: http://blogs.mathworks.com/pick/2007/12/28/matlab-basics-guis-without-guide/
If you want an easier time then see this:
or just start using this framework which has all the basic controls and all you have to do it to plug your code into the callbacks. http://www.mathworks.com/matlabcentral/fileexchange/24224-magic-matlab-generic-imaging-component
Thank you very much for the detailed answer Image analyst. I really appreciate it. However, out is the output of the main function. I asked out to change with every slider change. With the way you propose the theResult, which is the output of the second function, is the only one that changes when the slider value changes. The way I only see this happening is by using assignin.
function out=aGui
a=0;
uicontrol( ...
'Style' , 'Slider' , ...
'max' , 10 , ...
'min' , 0 , ...
'value' , 0 , ...
'sliderstep', [1/10 1/10] , ...
'units' , 'norm' , ...
'position' , [0.11 0.13 0.5 0.05] , ...
'Callback' , {@clbk, a});
end
function out=clbk(varargin)
sliderH=varargin{1};
a=varargin{3};
out=a-get(sliderH, 'value');
assignin('base', 'out', out)
end
You want the main function to be re-run each time the slider is changed? If not, then since "out" is the output of the main function, where should the new value of the slider be delivered to?
You're making this way too complicated. Just use GUIDE and it will be a lot simpler. You can have a callback that does stuff, such as create and/or modify other variables that are available to you via the link Walter gave you. Or, in your main program you can get the slider value directly whenever you need it with a simple line
sliderValue = get(handles.slider1, 'Value');
because it's value is stored in the "handles" structure which is pretty much global everywhere (at least to all the callbacks, though to your own custom functions only if you pass it in.) Please look at Doug Hull's tutorial.

Sign in to comment.

Before the get() of the value, insert a drawnow() or pause() to give a chance for the user to update the value. But consider also using waitfor() or uiwait(), or using a modal figure,

3 Comments

Thank you Walter but I don't understand how this solves my problem. Maybe I don't well the use of waitfor. Based on what I understood I did the following in the second function:
function theResult = clbk(varargin)
% It's code goes here...
% function res=clbk(varargin)
%
sliderH=varargin{1};
a=varargin{2};
waitfor(sliderH, 'value')
b=a-get(sliderH, 'value');
set(sliderH, 'userdata', b);
theResult=get(sliderH, 'userdata');
end
But in this case when I run the GUI, it waits till the slider changes value and then out becomes -1. If the slider changes again value, out does not become -2
Probably I have misunderstood your suggestions.
Each get() only fetches the value as of the time of that get(). If you later want the new value, then get() again. If you want "out" to be recalculated by the callback of the slider, then put the calculation there but share "out" with the places you need the value. See

Sign in to comment.

Categories

Find more on Graphics Objects 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!