MATLAB Answers

How to add animatedline to formatted figure?

2 views (last 30 days)
arianne ford
arianne ford on 24 Mar 2021
Commented: arianne ford on 25 Mar 2021
Hi all,
I am trying to get an animated line to plot on an already formatted figure. Simplified code below:
figure();
xlabel ('x');
ylabel ('y');
axis ([0 10000 0 10]);
grid on;
title('my plot', 12);
myline1 = animatedline('Color', 'r');
a = 0;
b = 1000000;
while a<b
x1 = somevalue;
y1 = someothervalue;
set(0, 'CurrentFigure', 1);
addpoints(myline1, x1, y1)
drawnow
a = a+1;
end
However what I'm getting from this is a nicely formatted figure with the axis titles and the range of values but empty of data points, and the animatedline plotting in another figure window. How can I get the animated line to plot onto the existing figure with the required formatting?
The code outputs the correct values for x1 and y1, it's just the plot that I'm struggling with.
Apologies if I'm missing the obvious, this is the first time I've used Matlab in nearly 20 years. Trying to relearn!
Would appreciate any advice.

Accepted Answer

Les Beckham
Les Beckham on 25 Mar 2021
From what you have posted, you have explicitly requested a new figure with the line:
figure();
This will create a new figure and subsequent plotting commands will put their output into this new figure, not any previously created figure.
Consider removing the figure() line and replacing by
hold on
It also sounds like you are having issues with the "nicely formatted figure" since you say it is "empty of data points".
If you wish to get help with that you will need to post the code that you are using to create that as well.
  1 Comment
arianne ford
arianne ford on 25 Mar 2021
I think I have it functioning now. The domain I was using for the y-axis was insufficient. Increased it by a few orders of magnitude and now I can see the line.
Thanks for the tip regardless!

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!