How to create a new figure in MATLAB?

246,261

Solution 1

figure;
plot(something);

or

figure(2);
plot(something);
...
figure(3);
plot(something else);
...

etc.

Solution 2

While doing "figure(1), figure(2),..." will solve the problem in most cases, it will not solve them in all cases. Suppose you have a bunch of MATLAB figures on your desktop and how many you have open varies from time to time before you run your code. Using the answers provided, you will overwrite these figures, which you may not want. The easy workaround is to just use the command "figure" before you plot.

Example: you have five figures on your desktop from a previous script you ran and you use

figure(1);
plot(...)

figure(2);
plot(...)

You just plotted over the figures on your desktop. However the code

figure;
plot(...)

figure;
plot(...)

just created figures 6 and 7 with your desired plots and left your previous plots 1-5 alone.

Solution 3

The other thing to be careful about, is to use the clf (clear figure) command when you are starting a fresh plot. Otherwise you may be plotting on a pre-existing figure (not possible with the figure command by itself, but if you do figure(2) there may already be a figure #2), with more than one axis, or an axis that is placed kinda funny. Use clf to ensure that you're starting from scratch:

figure(N);
clf;
plot(something);
...

Solution 4

As has already been said: figure will create a new figure for your next plots. While calling figure you can also configure it. Example:

figHandle = figure('Name', 'Name of Figure', 'OuterPosition',[1, 1, scrsz(3), scrsz(4)]);

The example sets the name for the window and the outer size of it in relation to the used screen. Here figHandle is the handle to the resulting figure and can be used later to change appearance and content. Examples:

Dot notation:

figHandle.PaperOrientation = 'portrait';
figHandle.PaperUnits = 'centimeters';

Old Style:

set(figHandle, 'PaperOrientation', 'portrait', 'PaperUnits', 'centimeters');

Using the handle with dot notation or set, options for printing are configured here.

By keeping the handles for the figures with distinc names you can interact with multiple active figures. To set a existing figure as your active, call figure(figHandle). New plots will go there now.

Solution 5

Another common option is when you do want multiple plots in a single window

f = figure;
hold on
plot(x1,y1)
plot(x2,y2)
...

plots multiple data sets on the same (new) figure.

Share:
246,261
Jader Dias
Author by

Jader Dias

Perl, Javascript, C#, Go, Matlab and Python Developer

Updated on August 03, 2020

Comments

  • Jader Dias
    Jader Dias almost 4 years

    Usually when I plot in MATLAB, it always draws on the same figure. How do I make it draw in a new figure?

    I know it is pretty elementary, but I'm not finding it using Google Search.

  • CKM
    CKM over 8 years
    Is there some setting in MATLAB so that every time I issue the command plot, it plots on new figure window without specifically typing figure(n) followed by plot?
  • Ka Wa Yip
    Ka Wa Yip almost 8 years
    @Federico A. Ramponi Why not start with figure(1), but start with figure(2)?
  • Grebu
    Grebu almost 8 years
    @Masi: This should still be possible, but it could be that it only works with figure('Name', name) - name being a string here. Could you test that? With the recent Matlab versions you can also access the figures properties via the dot notation: figure.Name = 'myFigureName'
  • Grebu
    Grebu almost 8 years
    Try using figure('Name', 'Name of figure'). The first string is the property and the second the value to use for it. The dot convention is used to access one property at a time. I think it was not implemented for figure etc. in Matlab2012/13. However it could already be used by custom classes.