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

5 次查看(过去 30 天)
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

采纳的回答

Alan Moses
Alan Moses 2020-11-28
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!

更多回答(1 个)

Steven Lord
Steven Lord 2020-11-30
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 个评论
Peter Beringer
Peter Beringer 2020-11-30
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?)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Animation 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by