Question

Let us consider the following code (fs is sampling frequency)

function [] = AR_simulate(p,fs)

B = xlsread('data_generations1','A1','g8:g301');
for k = 1:1:p
    [pxx,f] = pyulear(B,k);
    plot(f,pxx);
    F(k) = getframe;
end
movie(F,100);

end

which animates the power spectral density of a given B vector with different orders, namely from order k=1 to k=p.

Now I want to save each picture somewhere, but on each picture I would like to include the parameter k, for example at k=1 there was this picture, at k=2 there was different picture and so on.

Was it helpful?

Solution

This will put the title of each frame as k=1, k=2 etc. and will print each frame as an eps file to Frame1.eps, Frame2.eps, etc.

function []=AR_simulate(p,fs);
B=xlsread('data_generations1','A1','g8:g301');
 for k=1:1:p
     [pxx,f]=pyulear(B,k);
     subplot(1,2,1)
     plot(f,pxx);
     F(k) = getframe;
     title(['k=' num2str(k)])
     subplot(1,2,2)
     % 2nd plot
     print('-depsc2',['Frame' num2str(k) '.eps')
 end
 movie(F,100);
end

You can also use text instead of title to place the text anywhere on the plot.

OTHER TIPS

By default, getframe with zero input arguments captures a frame based on the current axis. If you want to capture a frame based on the figure, including a title and the axes ticks, you need to do getframe(gcf). Alternatively, use text as suggested by @David to place the label on the axes you capture.

Regarding saving of the images, you can use frame2im and imwrite, in the loop just after getframe:

im = frame2im(F);
imwrite(im,sprintf('im_k%03d.png',k))

Or to save the frames to a movie, use the VideoWriter class and call writeVideo on every iteration.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top