Saving or using callback values

Hello,
So I am trying to plot a graph with a slider that goes from 1 to 10
f = figure(1);
hold on
h1.f = f;
h1.sld = uicontrol('style','slider','position',[80 20 460 30],...
'Min',0 , 'Max',10 , 'SliderStep',[0.01 0.1] , 'Value', 1 ) ;
h1.txt = uicontrol('style','text','position',[20 20 60 30],'String','1','Fontsize',12) ;
set(h1.sld,'Callback', {@sld_callback,h1.txt} )
I have a function that changes the text in the box in the figure based on what I select
function sld_callback(hobj,~, h_text)
val = get(hobj,'Value') ;
set(h_text,'String', num2str(val) )
end
But is there a way for me to save the hobj value? Say I move the slider till the optimum number, I want to be able to save the number for further data analysis.
Thank you.

2 个评论

Is the 'further data analysis' to be triggered by you letting go of the slider?
If not then what is wrong with just referring to
h1.sld.Value
later on in your code when you need it?
Sorry, I ought to have specified. I was hoping to move this slider to find the optimum value and save it. I am doing it to a 150 dataset, so it would be nice if I could run the rest of the data analysis without finding the optimum factor again in the future. Does that make sense?

请先登录,再进行评论。

回答(1 个)

Stephen23
Stephen23 2018-11-13
编辑:Stephen23 2018-11-13

0 个投票

The easiest way to do this is to use a nested function (which in this case is your callback function):
function val = myfun()
f = figure(1);
val = []; % must be defined in the parent workspace!
...
% nested function:
function callback(~,~, h_text)
val = ...
end
% wait until figure is closed:
waitfor(f)
end
You can easily add other callbcaks to save or process any values in the parent function's workspace. See my FEX submissions iregexp and cubehelix_view for examples of how to combine nested and local callback functions to process data:

1 个评论

Thank you.
Sorry to ask just one more question, so I tried your code and it does indeed outputs the value of the slider :) But I can't get my sld_callback to work as I adjust the slider. If I comment out the waitfor(f) part, i know my sld_callback will work as I adjust the slider, but callback would fail. Is there a way to let parts of it run while I move the slider?

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Cell Arrays 的更多信息

产品

版本

R2018b

标签

Community Treasure Hunt

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

Start Hunting!

Translated by