How to use a slider to move through plots

34 views (last 30 days)
I am plotting waveforms that come in at 10 per second. I currently have code that will shift through all of the waveforms collected, one by one, with a small delay between each one, so it almost looks like a stop motion movie. I would like to have something like a slider or the arrow keys to be able to sort through my data back ad forth. I am having troubles though because I have never coded for a slider or push button before. Any help or ideas would be greatly appreciated.
  3 Comments
dpb
dpb on 1 Jul 2017
So you use a |uicontrol('Style','slider',... and have the callback function set the index to the array start point instead of using the loop.
You'll undoubtedly want to compute the number of waveforms from the total number of points/length(each) and use that as the slider variable instead of absolute points, but it should be pretty straightforward.
Start with
doc uicontrol
and see how the slider works with the example there and modify to suit...
Patrick Lydon
Patrick Lydon on 3 Jul 2017
Hi i tried implementing it and I keep getting an error, do you see what I have done wrong?
tt=1;
gg=NDataPoints;
h=plot(x(1,tt:gg),y1(1,tt:gg),'k');
uicontrol('Style', 'slider',...
'Min',1,'Max',NDataPoints,'Value',1,...
'Position', [400 20 120 20],...
'Callback', {@src,hax});
function src(source,event)
val = get(source, 'Value');
set(f,'XData',XData(1,(NDataPoints*val)-(NDataPoints-1):NDataPoints*val);

Sign in to comment.

Accepted Answer

dpb
dpb on 4 Jul 2017
Edited: dpb on 4 Jul 2017
Well, the following runs but isn't yet ready for prime time...needs some "prettying-up" to move the slider out of the way from occluding the axes; would probably be worth putting a text box to display the trace number and range and a few other niceties, but it does work...
function lydon(x,y)
hFig = figure('Visible','off');
hAx = axes;
tt=1;
NDataPoints=100;
gg=NDataPoints;
nTraces=fix(length(y)/NDataPoints); % no full traces in dataset
ss1=1/(nTraces-1); % scale to move a trace at a time
hL=plot(x(tt:gg),y(tt:gg),'k');
uicontrol('Style', 'slider',...
'Min',1, ...
'Max',nTraces, ...
'Value',1,...
'SliderStep',[ss1 ss1], ...
'Position', [400 20 120 20],...
'Callback', @src);
hFig.Visible='on'
function src(source,event)
val = get(source, 'Value');
i1=1+(val-1)*NDataPoints;
i2=val*NDataPoints;
set(hL,'XData',x(i1:i2),'YData',y(i1:i2))
end
end
My release doesn't yet support functions in script files so to have everything global I used the internal function within the outer function. You thus must call the main function with the two argument x,y vectors.
This works to make the slider move in increments of 1:nTraces in the overall input vector to move from one to the next and not have to worry about whether the indices were integer or not.
Using 1/(nTraces-1) as the minor and major step values means each click moves one trace; you could make the major a multiple of the minor if there are typically so many that the movement in single trace increments gets too tedious but for the 1:10 range I used for testing it works pretty well this way.
The biggest problem in your original was in not following the exact outline of the example and having two arguments in the callback instead of just the callback function handle...with that it then ran; rest was cleaning up initial values and the index calculations.

More Answers (1)

Walter Roberson
Walter Roberson on 4 Jul 2017
  1 Comment
dpb
dpb on 4 Jul 2017
And much nicer looking it is, too! :) I just made Patrick's outline run (and have to admit it's the first uicontrol callback I've ever written (er, debugged/modified)..."I don't do windows" :)

Sign in to comment.

Categories

Find more on Graphics Object Programming 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!