Clear Filters
Clear Filters

'hold off' produces second plot in MATLAB Live Script

24 views (last 30 days)
I find a strange behaviour if I use 'hold on' and 'hold off' in a MATLAB Live Script. I tried out R2022b and R2023b.
If I put all my code into the first cell, it works as expected, i.e. it makes one plot.
But if I call 'hold off' in a new cell, it creates an additional plot.
First cell:
plot([1 2 3 4 5])
hold on;
plot([5 4 3 2 1 ])
Second cell:
hold off
yields TWO plots. Is this a bug? I always thought that 'hold on/off' is just a switch variable...

Accepted Answer

Zinea
Zinea on 30 Sep 2024
The behaviour stated in the question is not a bug, rather it is an expected behaviour. The following points are to be noted in case of Live Scripts in MATLAB:
  1. Each cell in a Live Script can be treated as a separate execution unit. When a cell is run, MATLAB processes the commands within it, and if there's a plotting command, it generates a plot in the output area.
  2. The "hold on" command tells MATLAB to retain the current plot and all its properties so that subsequent plots are added to the existing axes. "hold off" resets this behavior, allowing new plots to replace the existing one.
  3. When the first cell is executed, it plots the two lines on the same axes due to hold on. When the second cell is executed containing only "hold off", MATLAB considers this as a separate execution, which leads to generating a new figure if the Live Script environment interprets the "hold off" as a standalone command without a preceding plot.
NOTE: Replacing the "hold off" by "hold on" in the separate cells would also result in plotting of separate plots.
Hope this resolves your query, cheers!
  3 Comments
Zinea
Zinea on 7 Oct 2024 at 14:22
I think for the first scenario you meant:
"THREE diagrams, once the first and twice the second"
When calling hold off at the section break, the Live Editor tries to preserve the state of the current runtime figure by cloning it. This is because the content of the figure may be cleared. That explains the first scenario above.
The second scenario creates two figures as it is a MATLAB script instead of a MATLAB live script.
The third scenario occurs because when multiple commands are written in the same line in a live script, the commands run in order from left to right and each command overwrites the behaviour of the previous command. Hence, the output of only the last command is displayed, resulting in ONE figure. This behaviour may be checked by the following examples:
Example 1: The following when written in one cell, results in TWO figures, one figure for each line:
plot([1 3]);
plot([3 1]);
Example 2: The above commands when written inthe same line result in ONE figure, which is the output of the rightmost command:
plot([1 3]);plot([3 1]);
Cornelia
Cornelia on 14 Oct 2024 at 7:07
Hi @Zinea, thank you again for your answer.
No, I really mean "THREE diagrams, twice the first and once the second" and now I understand why:
  • The first plot(...) creates the first diagram
  • The "hold off" in the next cell shows again the FIRST diagram that is still in MATLAB's memory at that moment
  • The second plot(...) creates the second diagram
I still don't like this behaviour of "hold off", for me it is counter-intuitive, but okay we cannot change ;-)
Best regards
Cornelia

Sign in to comment.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!