Elisa m
Elisa m

Reputation: 145

How can i create figures in a foor loop?

I have to create a figure for every 80 measurements of a row vector Bn with length 31999. I tried to write this code but I receive only one figure with all the measurements (31999):

k= length(Bn);
for i= 1:80:(k-1)
    Bn1(i) = L(i);
    plot(Bn1);
end

any suggestion?

Upvotes: 0

Views: 64

Answers (2)

Cris Luengo
Cris Luengo

Reputation: 60444

Given a vector Bn, you can extract 80 values starting at index ii using Bn(ii:ii+79). Within your loop, you thus need to plot those values only.

However, this will create 400 figure windows, which is unmanageable. I recommend you save the plots to file instead:

figure
k = numel(Bn);
for ii = 1:80:k
    plot(Bn(ii:ii+79));
    print('-dpng','-r0',sprintf('plot%.3d',ii))
end

The plot command will overwrite the previous plot each time.

I recommend you look through the documentation for print to learn about the options you have there (different file formats -d and resolutions -r).

Upvotes: 1

Tyler Klein
Tyler Klein

Reputation: 60

If you want to create one figure for each element, just insert the figure command inside the loop

k= length(Bn);
for i= 1:80:(k-1)
    figure % this is what you need
    Bn1(i) = L(i);
    plot(Bn1);
end

I'm not sure what L is, so I can't tell if that will do what you expect, but that will create a new figure for each iteration of the loop.

Upvotes: 0

Related Questions