user466534
user466534

Reputation:

save figures in matlab during loop

I am interested if it is possible to save figures which occurs during the loop.
For example, I have created some random matrix,

 r=rand(8,5)


r =

0.8147    0.9575    0.4218    0.6787    0.2769
0.9058    0.9649    0.9157    0.7577    0.0462
0.1270    0.1576    0.7922    0.7431    0.0971
0.9134    0.9706    0.9595    0.3922    0.8235
0.6324    0.9572    0.6557    0.6555    0.6948
0.0975    0.4854    0.0357    0.1712    0.3171
0.2785    0.8003    0.8491    0.7060    0.9502
0.5469    0.1419    0.9340    0.0318    0.0344

now if I use this line

plot(r(1,:))

I get following figure

enter image description here

My question is, if I use loop

for i=1:8
   plot(r(i,:))
end

it shows me one graph of row, but it odes not do cycle, so can I show all 8 figures step by step in matlab. For exmaple interval may be 10 seconds, for this as I know function movie is used, also getframe, or could I save figures in loop?
Also, I know that imsave or something like this. I think better would be save somewhere, so if I can use like this

imsave(plot(r(i,:))

Upvotes: 2

Views: 2768

Answers (3)

Roney Michael
Roney Michael

Reputation: 3994

If you mean that you want to display all the plots by using a loop, you can do the following:

for ii=1:8
    figure();
    plot(r(ii,:));
end

Upvotes: 1

Dr_Sam
Dr_Sam

Reputation: 1878

I think that there are multiple solutions to your problem:

  • Use the pause function, which can make a pause so that you can look at your figure.
  • Print each image in a new figure. To achieve this, just put figure(i) in your for loop.
  • Or save the image using the print function.

Hope it helps!

Upvotes: 1

Shai
Shai

Reputation: 114786

use getframe to capture the content of the current figure handle

fh = figure
% ...
for ii = 1:8
    figure( fh ); % focus on figure
    plot( r(ii,:) );
    frm = getframe( fh );
    % save as png image
    imwrite( frm.cdata, sprintf( 'current_frame_%02d.png', ii ) );
end

PS
It is best not to use i as a variable in Matlab

Upvotes: 5

Related Questions