Saving or using callback values
Show older comments
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 Comments
Adam
on 13 Nov 2018
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?
Belinda Ding
on 13 Nov 2018
Answers (1)
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 Comment
Belinda Ding
on 13 Nov 2018
Categories
Find more on Cell Arrays 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!