Simple, but stumped. Using 'xline' to create animated "play marker" across waveform plot.

15 views (last 30 days)
I am trying to create a plot of a waveform that has a "play marker"/vertical line running across. Have been trying with 'xline' drawing at position 'ii' in a for loop, but I can't figure out how to clear previous lines, so every line position stays on the plot. (Have included test data lines at the top so it can run.)
I know the solution is bound to be really simple, but it eludes me. Currently investigating whether the 'animatedline' function call is better suited.
Many thanks for any help.
Cheers.
Fs = 48000;
Position01 = rand(Fs,11);
StartIndex = 1;
FinishIndex = 100;
figure;
subplot(2,1,1);
plot(Position01(StartIndex:FinishIndex,11));
hold on
title('test');
xlabel('test');
ylabel('test');
subplot(2,1,2);
plot(Position01(StartIndex:FinishIndex,1));
title('test');
xlabel('test');
ylabel('test');
for ii = 1 : length(Position01(StartIndex:FinishIndex,11))
xline(ii);
drawnow
end

Accepted Answer

Alan Moses
Alan Moses on 28 Nov 2020
You may use the following lines of code to clear the previous line:
children = get(gca, 'children');
delete(children(1));
The explanation to the above lines of code can be found here.
The above lines of code can be added to your script as follows:
for ii = 1 : length(Position01(StartIndex:FinishIndex,11))
%Ensures the original plot stays and clears only the vertical lines plotted
if(ii >= 2)
children = get(gca, 'children');
delete(children(1));
end
xline(ii);
drawnow
end
Hope this helps!

More Answers (1)

Steven Lord
Steven Lord on 30 Nov 2020
Rather than deleting and recreating the line, change its properties.
% Define the data
x = 0:360;
y = sind(x);
% Create three plots
plot(x, y); % Main sine curve
hold on
h = xline(x(1)); % vertical cursor
m = plot(x(1), y(1), 'ro'); % intersection circle
% Update the h and m lines
for whichX = 1:10:numel(x)
h.Value = x(whichX);
m.XData = x(whichX);
m.YData = y(whichX);
pause(0.25)
end
  1 Comment
Peter Beringer
Peter Beringer on 30 Nov 2020
Wow, thanks. I actually find that a more elegant and efficient solution. And the intersection marker circle is a useful touch for my purposes. Thanks a lot!
(Is it possible to accept more than one answer?)

Sign in to comment.

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!