Getting the output from a callback in main function

3 ビュー (過去 30 日間)
Giorgos Papakonstantinou
Giorgos Papakonstantinou 2013 年 9 月 15 日
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.

回答 (2 件)

Image Analyst
Image Analyst 2013 年 9 月 15 日
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 件のコメント
Walter Roberson
Walter Roberson 2013 年 9 月 16 日
編集済み: Image Analyst 2013 年 9 月 16 日
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?
Image Analyst
Image Analyst 2013 年 9 月 16 日
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.

サインインしてコメントする。


Walter Roberson
Walter Roberson 2013 年 9 月 15 日
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 件のコメント
Giorgos Papakonstantinou
Giorgos Papakonstantinou 2013 年 9 月 15 日
Probably I have misunderstood your suggestions.
Walter Roberson
Walter Roberson 2013 年 9 月 15 日
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

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGraphics Objects についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by