Make a smooth animation

Hallo everybody.
I need some help with the following: I have to create an animation, of several plots.
BUT, I CANNOT use the standard procedure of having a for loop, and inside the for plot, because the result is something which is not smooth. Probably due to the computation complexity, sometimes it lags...
Therefore, I was thinking about making a video out of the desired animation, and then play the video. The problem is though, that I DO NOT want to play the animation first, and then play the video. I only want to play the video, which has to be a result of several plots.
So in essence:
for i=1:frames
plot(...) %-> I would like this plot not to be visualized.
M(i)=getframe(gca);
end
In the plot command, I would like not to plot and update the axes component. I just want somehow to export the image (frame) that would be the result of this command, and then save it as a frame for my video. (Without actually plotting).
Any help would be greatly appreciated... :)

 Accepted Answer

Angus
Angus on 7 Jun 2013
Use the 'visible' setting, initially set the figure
f = figure('visible','off')
and if you want to change it at some point then
set(f,'visible','on')
This will make things faster to compute as it does not have to update the display as it runs. It will still be updating the plot and axes but you just wont see it.

5 Comments

I tried doing so, but I got as a result a very annoying, moment of closing and then reappearing of the figure.
Which is logical (since this is what I am going to do by setting the visible to off and then on again).
Is there no other way to achieve this, without setting the visible property off and then on again. Somehow to draw directly to an image and the use this image as frame? (without having to use the plot command?)
I see, that was not what I expected. Well if you are ok with writing to .avi then here is another option.
avi = avifile('example.avi');
f = figure('visible', 'off');
for i = 1:frames
plot(...)
avi = addframe(avi,f);
end
avi = close(avi)
implay('example.avi')
Turns out this does not work, because addframe needs as input a frame, so there is the need to use getframe. And get frame screws up, because when I use it, it sets the figure back to visible. I tried setting it back to off, inside the loop, but it reeeeally doesn't work!
So basically, the question comes to whether it is possible to export the data that describe a visual object (line-rect-etc...), directly to a picture, and not having to go through the procedure of plotting->getting frame.
Sorry if im missing something about what precisely you are plotting, or maybe its a version issue or something else. If this does not work, im not too sure what other options you might have, there is likely some workable alternate to plot.
This code runs as is for me, only displaying the finished video in a gui at the end. Let me know if this works or the issue is something else.
avi = avifile('example.avi');
f = figure('visible', 'off');
frames = 200;
x = -pi:.1:pi;
y = sin(x);
for i = 1:frames
plot(x,y)
avi = addframe(avi,f);
y = sin(x+(i/pi));
end
avi = close(avi)
implay('example.avi')
The addframe function is able to take the figure handle as an input and does not require getframe at any point. Hope this works. Cheers.
thank you very much for your help! I really appreciate your help!

Sign in to comment.

More Answers (0)

Categories

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!