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 件のコメント

Adam
Adam 2018 年 11 月 13 日
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 2018 年 11 月 13 日
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 件のコメント

Belinda Ding
Belinda Ding 2018 年 11 月 13 日
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 ExchangeCell Arrays についてさらに検索

製品

リリース

R2018b

タグ

質問済み:

2018 年 11 月 13 日

コメント済み:

2018 年 11 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by