'hold off' produces second plot in MATLAB Live Script (2024)

81 views (last 30 days)

Show older comments

Cornelia on 30 Sep 2024 at 14:50

  • Link

    Direct link to this question

    https://uk.mathworks.com/matlabcentral/answers/2156465-hold-off-produces-second-plot-in-matlab-live-script

  • Link

    Direct link to this question

    https://uk.mathworks.com/matlabcentral/answers/2156465-hold-off-produces-second-plot-in-matlab-live-script

Commented: Zinea on 7 Oct 2024 at 14:22

  • Test.mlx

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...

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Answers (1)

Zinea on 30 Sep 2024 at 15:55

  • Link

    Direct link to this answer

    https://uk.mathworks.com/matlabcentral/answers/2156465-hold-off-produces-second-plot-in-matlab-live-script#answer_1524870

  • Link

    Direct link to this answer

    https://uk.mathworks.com/matlabcentral/answers/2156465-hold-off-produces-second-plot-in-matlab-live-script#answer_1524870

Hi Cornelia,

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!

2 Comments

Show NoneHide None

Cornelia on 1 Oct 2024 at 12:58

Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/2156465-hold-off-produces-second-plot-in-matlab-live-script#comment_3273065

Hi @Zinea,

Thank you for your answer. Your explanation matches exactly my impressions, still I do not fully understand the logic behind. I built up another test case:

First cell:

plot([1 2])

Second cell:

hold off;

plot([2 1])

This creates THREE diagrams, twice the first and once the second.

It does not matter if I put the semicolon or not.

-----------------------------------------

Then I put the content of the second cell into a file "myTest.m" and run

First cell:

plot([1 2])

Second cell:

myTest

This creates TWO diagrams, once the first and once the second.

----------------------------------------

Then I put the code of my second cell into one line

First cell:

plot([1 2])

Second cell:

hold off; plot([2 1])

This again creates TWO diagrams, once the first and once the second.

----------------------------------------

I am an experienced MATLAB user, but for me it feels like a bug to get THREE diagrams in the first case.

Zinea on 7 Oct 2024 at 14:22

Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/2156465-hold-off-produces-second-plot-in-matlab-live-script#comment_3277775

  • Link

    Direct link to this comment

    https://uk.mathworks.com/matlabcentral/answers/2156465-hold-off-produces-second-plot-in-matlab-live-script#comment_3277775

Open in MATLAB Online

Hi @Cornelia,

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]);

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABGraphicsFormatting and AnnotationLabels and AnnotationsAnnotations

Find more on Annotations in Help Center and File Exchange

Tags

  • live script
  • hold

Products

  • MATLAB

Release

R2023b

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


'hold off' produces second plot in MATLAB Live Script (5)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

'hold off' produces second plot in MATLAB Live Script (2024)
Top Articles
Latest Posts
Recommended Articles
Article information

Author: Otha Schamberger

Last Updated:

Views: 6095

Rating: 4.4 / 5 (75 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Otha Schamberger

Birthday: 1999-08-15

Address: Suite 490 606 Hammes Ferry, Carterhaven, IL 62290

Phone: +8557035444877

Job: Forward IT Agent

Hobby: Fishing, Flying, Jewelry making, Digital arts, Sand art, Parkour, tabletop games

Introduction: My name is Otha Schamberger, I am a vast, good, healthy, cheerful, energetic, gorgeous, magnificent person who loves writing and wants to share my knowledge and understanding with you.