1

I want to plot several figures on matlab using a for loop. I tried the following:

figure; plot(toDplot) 

and

figure(2); plot(thing) figure(3); plot(something) 

etc

But in both cases, my first plot is replaced by the next one...

Why is it replacing the first frames?

7
  • so you're using figure(x) inside the for-loop? but always with the same number x? Use a different number every iteration and it will create a new figure, or use hold on behind the plot command, to plot multiple graphs in the same figure. Commented Sep 30, 2013 at 22:32
  • I'm using figure(k) (when I loop on k). I also tried with hold on but it plots me the different curbs on the same plot and I want each curb to be plotted on a different figure Commented Sep 30, 2013 at 23:10
  • So you end up with lots of empty figures and only one with your plot? Commented Sep 30, 2013 at 23:13
  • 1
    If you type in the command window the following: close all; figure; figure;, does it create 2 figures? Commented Sep 30, 2013 at 23:35
  • 1
    How about figure(1); figure(2);? Two figures? Commented Sep 30, 2013 at 23:59

1 Answer 1

1

Your new figures are not taking over as the current figure (gcf).

To explicitly specify where a plot will go, you can call it with the syntax plot(HA,...). From the MATLAB docs:

plot(axes_handle,___) plots into the axes specified by axes_handle instead of into the current axes (gca) 

To use this, you would make a figure and axes, storing their handles, like so:

hf = figure; ha = axes('parent',hf); plot(ha,x,y) 

P.S. I just saw that you got it to work by putting a close all before your loop! I'll keep the answer here for reference since it is a good way to be explicit with your plotting.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.