フィルターのクリア

How to make a slider GUI with most simple code

30 ビュー (過去 30 日間)
Yode
Yode 2017 年 7 月 7 日
コメント済み: Chris Deep 2022 年 4 月 6 日
I'm a Mathematica user,so I can make a slider gui with this code
Manipulate[Plot[Sin[a x], {x, 0, 2 Pi}], {a, 1, 2}]
Recently,I'm learning Matlab,the powerful software as my teacher say.But I realize I have to write a ton code for such target
function sliderSin
f=figure('visible','off','position',...
[360 500 400 400]);
slhan=uicontrol('style','slider','position',[100 280 200 20],...
'min',0,'max',4*pi,'callback',@callbackfn);
hsttext=uicontrol('style','text',...
'position',[170 340 40 15],'visible','off');
axes('units','pixels','position',[100 50 200 200]);
movegui(f,'center')
set(f,'visible','on');
function callbackfn(source,eventdata)
num=get(slhan,'value');
set(hsttext,'visible','on','string',num2str(num))
x=linspace(0,4*pi);
y=sin(num*x);
plot(x,y);
ax=gca;
ax.XLim=[0 2*pi]
end
end
Then I will get a slider gui like
The current solution have two problem make me crazy:
  • I have to write so many code?
  • Why it cannot run normally when I drag the slider?
Help please.
  2 件のコメント
Adam
Adam 2017 年 7 月 7 日
編集済み: Adam 2017 年 7 月 7 日
There's probably as much code to do it in Mathematica, just that it is written for you.
In the same way my colleagues have access to slider classes that combine a slider (including Java sliders that naturally have the continuous callback you are looking for and look nicer than Matlab sliders) with an edit box to update each other and allow various functionality with simple setup of slider steps and related things. They only have that though because I spent a long time putting together the functionality, correcting it, tuning it, integrating java sliders, spin controls etc to give nicely usable components.
So, the moral of the story in any language - some things are written for you, some things you have to write yourself, but if you do it with some thought and in a reusable way you only have to do it once, wrap it in a nicely usable interface and then use it as many times as you like.
Yode
Yode 2017 年 7 月 7 日
@Adam It is a good lesson for me.Thanks very much. :)

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

採用された回答

Jan
Jan 2017 年 7 月 7 日
編集済み: Jan 2017 年 7 月 7 日
  • Why it cannot run normally when I drag the slider?
This is "normally". The code is using the Callback currently, which is triggered, when the mouse is released. For a continuous callback, use the listener: https://www.mathworks.com/matlabcentral/answers/264979-continuous-slider-callback-how-to-get-value-from-addlistener#answer_207231 .
  • I have to write so many code?
Yes. This is Matlab. Somethings are easier than in Mathematica, some things are harder.
It would save some code lines and makes the animation more fluently, if you create the line object once and update the YData only:
function sliderSin
FigH = figure('position',[360 500 400 400]);
axes('XLim', [0 4*pi], 'units','pixels', ...
'position',[100 50 200 200], 'NextPlot', 'add');
x = linspace(0, 4*pi, 400);
y = sin(x);
LineH = plot(x,y);
TextH = uicontrol('style','text',...
'position',[170 340 40 15]);
SliderH = uicontrol('style','slider','position',[100 280 200 20],...
'min', 0, 'max', 4*pi);
addlistener(SliderH, 'Value', 'PostSet', @callbackfn);
movegui(FigH, 'center')
function callbackfn(source, eventdata)
num = get(eventdata.AffectedObject, 'Value');
LineH.YData = sin(num * x);
TextH.String = num2str(num);
end
end
  1 件のコメント
Yode
Yode 2017 年 7 月 7 日
Thanks a lot.If there is not better solution,I will accept yours. :)

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

その他の回答 (2 件)

Geoff Hayes
Geoff Hayes 2017 年 7 月 7 日
Yode - the slider callback (as you've defined) only fires when the slider stops moving and you want the callback to fire whenever there is a change to the slider position. To get this behaviour, you can add a listener (see addlistener to your slider so that whenever there is a change the slider position (or its Value property) then your callback fires. To do this, remove the callback from your slider creation so that you just have
slhan=uicontrol('style','slider','position',[100 280 200 20],'min',0,'max',4*pi);
Then add a listener for this slider that will invoke the callback whenever Value changes
hSliderListener = addlistener(slhan, 'Value', 'PostSet', @callbackfn);
Try the above and see what happens!
  2 件のコメント
Yode
Yode 2017 年 7 月 7 日
Thanks very much.
Grigorii Tarasov
Grigorii Tarasov 2018 年 7 月 30 日
Cannot execute this:
Error in sliderSin (line 5) num = get(eventdata.AffectedObject, 'Value');

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


Maria Pia Younger
Maria Pia Younger 2019 年 12 月 1 日
Thank you!
  1 件のコメント
Chris Deep
Chris Deep 2022 年 4 月 6 日
Useful stuff thanks for posting.
Only problem i had was that the following line needed to be before "LineH=plot(x,y);". Otherwise the slider did not show up.
SliderH=uicontrol('style',........);

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

カテゴリ

Help Center および File Exchange交互式控件和回调 についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!