2

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') 
9
  • 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 Commented Nov 7, 2013 at 16:10
  • 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. Commented Nov 7, 2013 at 16:20
  • 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. Commented Nov 7, 2013 at 16:56
  • 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 Commented Nov 7, 2013 at 17:10
  • 1
    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. Commented Nov 7, 2013 at 18:43

1 Answer 1

3

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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.