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