2
votes

I want to export my data in two figures with four subplots each. But when I try to do this inside a loop it keeps printing only the second figure with four plots. And when I use figure, it prints eight figures with one plot each. Here is the part of code:

subplot(2,2,k);
plot(2.^[4:2:10], a, '-mo', 2.^[4:2:10], b, '-r+', 2.^[4:2:10], c, '-bx'  );
axis([2.^4, 2.^10, 0, max([max(a), max(b), max(c)])]);    
str = sprintf('Time for m1 = 2^%d',i);
title(str);
xlabel('n ')
ylabel('s') 

subplot(2,2,k);
plot(2.^[4:2:10],a1, '-mo', 2.^[4:2:10], b1, '-r+', 2.^[4:2:10], c1, '-bx'  );
axis([2.^4, 2.^10, 0, max([max(a1), max(b1), max(c1)])]);    
str = sprintf('Time for m1 = 2^%d',i);
title(str);
xlabel('n ')
ylabel('M') 
1
Show the export part in your code. In which part do you do the exporting exactly? Also include figure and the essential part of the loop - Luis Mendo
Where is your loop? Maybe you could figure this out or get a bit further by creating a little demo that actually runs to illustrating what your code is doing. - horchler
have a look at my last edit, it's a complete general solution now. you can use it for every number of plots per figure. - Robert Seifert
when im trying to run this one Error using figure Requested figure handle in use by another object i already have a loop for making calculation and in the end i want two figures with 4 plot each to be printed .for example for i=1:1:4 %calculations figure1.......... figure2.......... end - l.g.karolos
yes, you just need introduce another index variable within your loop which distributes your curves into the right plots. Feel free to accept the answer, if it helped you. - Robert Seifert

1 Answers

3
votes

your loop needs to look somehow like this:

x = 1:2;
y = x;

f = 2;  %number of figures
c = 2;  %number of plots per column per figure
r = 2;  %number of plots per row per figure
n = repmat(cumsum(ones(1,r*c)),1,f);  %index for subplots
h = ceil( (1:f*r*c)/(r*c) ); %index of figures

for ii=1:f*r*c

   % calculations

   % plot specifier
   figure( h(ii) )
   subplot( r,c,n(ii) )

   % plot
   plot(x,y)

   % your plot properties
end

it gives you a figure(1) with 2x2 subplots and a figure(2) with 2x2 subplots

and for example

f = 3;  %number figures
c = 3;  %number of columns per figure
r = 4;  %number of rows per figure

would give you 3 figures with 3x4 plots each and so on...


If the order in which the plots appear matters, you can change the way h and n are created. These are just examples. Basically they are just vectors relating your index ii with the appearance order.