MATLAB Answers

Animated PLOT in GUI

15 views (last 30 days)
Adam Horvath
Adam Horvath on 29 Apr 2020
Edited: Adam Danz on 4 May 2020
Hello Everyone.
I am working on a gui with 4 different animated plots. I got 4 functions for 4 plots and do the animation like:
animatedline command..
for i=1:..
But Matlab has a warning that the axes(...) should be outside of the "for" iteration. How could I fix it?

  1 Comment

Geoff Hayes
Geoff Hayes on 29 Apr 2020
Adam - what is the full warning message? Please copy and paste all of the text to this question. And does it apply to all calls to axes or just one?

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 29 Apr 2020
Edited: Adam Danz on 4 May 2020
Here's an example the warning message that appears when calling axes() within a loop.
You're using the axes() command to make a selected axes current so your plotting function plots on the selected axes. There are better ways to do that.
Best method
Don't worry about which axis is current and use axis handles in all of your plotting functions so they are applied to the correct axes. Example:
plot(handles.axes1, x, y, '-o')
Alternative method
Set the current figure and then set the current axes within the current figure. Do this just before you do the plotting.
% fig is the handle to your figure
% ax is the handle to an axes within fig
set(0, 'CurrentFigure', fig)
set(fig, 'CurrentAxes', ax)
If the plotting code is lengthy and a user clicks on a different figure or axes after setting the the current axes and before the plotting is complete, the new graphics objects will be assigned to the wrong axes. This is why the first method is much better and fail-proof. The same problem occurs with the axes() solution from your question.


Sign in to comment.

Translated by