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 Kommentare

Adam
Adam am 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
Belinda Ding am 13 Nov. 2018
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?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Stephen23
Stephen23 am 13 Nov. 2018
Bearbeitet: Stephen23 am 13 Nov. 2018

0 Stimmen

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 Kommentar

Belinda Ding
Belinda Ding am 13 Nov. 2018
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?

Melden Sie sich an, um zu kommentieren.

Kategorien

Produkte

Version

R2018b

Tags

Gefragt:

am 13 Nov. 2018

Kommentiert:

am 13 Nov. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by