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

9 ビュー (過去 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 Center および File ExchangeAnnotations についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by