MATLAB Answers

2

How to constantly update a plot off of a slider being pulled

Lawson Hoover さんによって質問されました 2012 年 12 月 11 日
最新アクティビティ Amanda K Hanson さんによって コメントされました 2019 年 4 月 9 日
Is it possible to automatically update a graph based off of the slider. I now how to update the graph once the position in the slider has changed, but what I want to know is it possible to have the graph up date as the slider is being pulled. Essentially, as the slider is being pulled, I want the graph to update for each position that the slider value cross, not just update once the slider is let go off and one set value is made. Does anyone have any ideas how to do this?

  1 件のコメント

Srikanth Kothamasu 2016 年 3 月 10 日
I have two curves one is actual like (y=mx^2),other one is simulated curve which will be having similar behavior but not follow the actual one. so now i have to fallow the first curve with the help of slider is that possible. if so can you help out.

サインイン to comment.

2 件の回答

回答者: Teja Muppirala
2012 年 12 月 11 日
編集済み: John Kelly 2013 年 11 月 19 日
 採用された回答

I know what you are trying to do, I often want to do the same
Save the following in a file and run it to see an example:
function myslider
x = 1:10;
hplot = plot(x,0*x);
h = uicontrol('style','slider','units','pixel','position',[20 20 300 20]);
addlistener(h,'ActionEvent',@(hObject, event) makeplot(hObject, event,x,hplot));
function makeplot(hObject,event,x,hplot)
n = get(hObject,'Value');
set(hplot,'ydata',x.^n);
drawnow;

  3 件のコメント

Lawson Hoover 2012 年 12 月 11 日
Thank you for helping me! I was looking into the java stuff and I just wouldn't have the time to learn all that. What I am not fully clear on is:
@(hObject, event) mzkdplot(hObject, event,x,hplot));
zero
2016 年 11 月 27 日
Actually, I don't think you have plot the result with "real time". For the same result as yours, we could still use the "Callback" function of uicontrol. A good answer, while a perfect solution to "real time". Of course, I have not solved it, and still try.
Amanda K Hanson 2019 年 4 月 9 日
if you use uislider, you can use a value changED function (which would do what you already have) or a value changING function (which it appears you want). more info at https://www.mathworks.com/help/matlab/ref/uislider.html
If youre set on using uicontrol's slider, I'm not sure how to update it constantly.

サインイン to comment.


Login_Name
回答者: Login_Name
2012 年 12 月 11 日
編集済み: Login_Name
2012 年 12 月 11 日

Yes.
function [] = slider_plot()
% Plot different plots according to slider location.
S.fh = figure('units','pixels',...
'position',[300 300 300 300],...
'menubar','none',...
'name','slider_plot',...
'numbertitle','off',...
'resize','off');
S.x = 0:.01:1; % For plotting.
S.ax = axes('unit','pix',...
'position',[20 80 260 210]);
S.LN = plot(S.x,S.x,'r');
S.sl = uicontrol('style','slide',...
'unit','pix',...
'position',[20 10 260 30],...
'min',1,'max',10,'val',1,...
'sliderstep',[1/20 1/20],...
'callback',{@sl_call,S});
function [] = sl_call(varargin)
% Callback for the slider.
[h,S] = varargin{[1,3]}; % calling handle and data structure.
set(S.LN,'ydata',S.x.^get(h,'value'))

  3 件のコメント

Lawson Hoover 2012 年 12 月 11 日
Right I now how to this, In fact I am already using that for the slider. What I am looking for is as you pull the slider, without actually letting go of it, the graph with update constantly. So if the slider starts at val 1 and then is drag to val 3 it seamlessly changes the graph from val 1 to val 2 to val 3 as the slider value crosses over that value.
Login_Name
2012 年 12 月 11 日
Oh I see. You mean if you click-and-hold on the slider knob itself and drag it instead of clicking in the trough. In that case, I don't think you can do it without accessing the underlying JAVA. The callback will not fire until a buttonrelease event.
Sorry, but I don't see any way to do it in pure MATLAB.
Lawson Hoover 2012 年 12 月 11 日
Oh, ok, I was thinking that it had to do something with java. How hard would it be to do that? I have not really messed around with the java aspect of MATLAB yet.

サインイン to comment.



Translated by