Thread Subject: Plotting in Matlab Notebook

Subject: Plotting in Matlab Notebook

From: Dane Fratanduono

Date: 13 Nov, 2008 20:46:02

Message: 1 of 3

I am trying to generate two plots within a loop in a matlab notebook. When the notebook executes, the plots are generated but only one of the figures is placed into the notebook and matlab closes the window of the other. This can be better understood with the simple code:

[x = 0:1:10;
y = 0:2:20;
figure(1)
plot(x,y,'HandleVisibility','on','Visible','on')
figure(2)
plot(y,x.*y,'HandleVisibility','on','Visible','on') ]

When executing this code as an entire Cell Group only one figure is placed within the text. How does one solve this problem without creating separate cell groups?

Subject: Plotting in Matlab Notebook

From: Walter Roberson

Date: 13 Nov, 2008 21:24:09

Message: 2 of 3

Dane Fratanduono wrote:
> I am trying to generate two plots within a loop in a matlab notebook. When the notebook
> executes, the plots are generated but only one of the figures is placed into the notebook
> and matlab closes the window of the other.

I don't know why that might happen; it isn't anything obvious in your code. I've never worked
with a matlab notebook (I don't recall hearing of them, to tell the truth. Mathematica
notebooks, yes...)

> This can be better understood with the simple code:
> [x = 0:1:10;
> y = 0:2:20;
> figure(1)
> plot(x,y,'HandleVisibility','on','Visible','on')
> figure(2)
> plot(y,x.*y,'HandleVisibility','on','Visible','on') ]
 
> When executing this code as an entire Cell Group only one figure is placed within the text.
> How does one solve this problem without creating separate cell groups?

Any time I have difficulties working with multiple plots, I always convert the code
to be explicit about the Parent of axes and of plots:

x = 0:1:10;
y = 0:2:20;
fig1 = figure(1);
ax1 = axes('Parent', fig1);
plot(ax1, x,y,'HandleVisibility','on','Visible','on');
fig2 = figure(2);
ax2 = axes('Parent', fig2);
plot(ax2, y,x.*y,'HandleVisibility','on','Visible','on');

Amongst other benefits, this makes debugging easier: if you are single-stepping through
code that works with figures, then *often* in the process of giving control back
to the command line, the debugger changes the idea of what the "current figure"
or "current axes" is. Specifying the figure or axes explicitly for plotting commands
ensures that graphics will go -exactly- where you intended them, instead of
-hoping- that no callbacks or Matlab library routines change the notion of
the current figure or axes (e.g., the way the dialog boxes do.)

--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Subject: Plotting in Matlab Notebook

From: Luke

Date: 20 Nov, 2008 00:35:03

Message: 3 of 3

I am having the same problem and unfortunately the code you suggested does not fix it:

Walter Roberson <roberson@hushmail.com> wrote in message
> x = 0:1:10;
> y = 0:2:20;
> fig1 = figure(1);
> ax1 = axes('Parent', fig1);
> plot(ax1, x,y,'HandleVisibility','on','Visible','on');
> fig2 = figure(2);
> ax2 = axes('Parent', fig2);
> plot(ax2, y,x.*y,'HandleVisibility','on','Visible','on');

If this code is typed into a notebook, both figures are created, but the first is destroyed and only the second graph is printed in the notebook. The same thing happens if you put this code in an m-file and call the m-file from the notebook.

Can anybody please explain how the notebook embeds figures? Why does it delete figures created using loops? Is there any way to get around this?

Thanks!

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
notebook Luke 19 Nov, 2008 19:35:09
notebook Dane Fratanduono 13 Nov, 2008 15:50:18
rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com