[Matlab] Making a movie from plots

AI Thread Summary
The discussion focuses on creating a movie from a spring pendulum model in Matlab, where the user successfully plots the trajectory of the bob but faces issues with overlapping plots. The user seeks a method to generate frames for the movie without losing the existing plots. Suggestions include separating the plotting and movie generation into different scripts and using figure handles to manage multiple plot windows. This approach allows for the preservation of the original plots while still creating an animated visualization of the pendulum's motion. The conversation emphasizes efficient coding practices in Matlab for better visualization and clarity.
Nick89
Messages
553
Reaction score
0
Hi,

This isn't really technically a homework question but I thought I'd still ask here...

I am solving a spring pendulum model (set of differential equations) using Matlab, and the homework assignment tells me to plot the trajectory of the bob in the x-y plane. Well, I did that, but I find it is fairly hard to see anything in the plot. I mean, it's a nice curvy line and all, but I can't really verify if it 'looks natural' or not.

So, I wanted to make it into a movie, using the following code:
Code:
% movie
for i = 1:length(X)
    plot(X(i),Y(i),'o'); axis([-10 10 -10 10]);
    line([0,X(i)],[0,Y(i)]);
    F(i) = getframe;
end
movie(F, 1, 25);
Here, X and Y are the x and y coordinates of the solution (motion of the bob).

Now, I can see the bob move up and down nicely while swinging left to right. It also draws the spring or suspension rope between the point (0,0) and (X,Y) each frame.

The problem however is that I don't want my script to actually PLOT each individual frame, because I am already plotting the x(t) vs time, y(t) vs time, and x(t) vs y(t) plots (as the assignment asks me to do). Now, these plots are lost because they're overwritten by the movie plots.

Can I not plot the plots 'into memory' and store them in the F frames array that way? Then, I can return the frames array F and let the user play the movie manually using the movie() command.

Is that possible?
 
Last edited:
Physics news on Phys.org
Your script is probably trying to accomplish too much at once!

Why not make one script for generating your plots, and one for generating / playing your movie? If you're really, really insistent; the figure command opens up a new plotting window. plot plots to the most recently opened plot window (if there is one), but you can plot to an arbitrary plot window using the window's handle:

>> h1 = figure; %h1 is now a handle into the new window you just opened.
>> h2 = figure;
>> plot([-10:0.1:10], [-10:0.1:10].^2); plots a parabola in the second window
>> pause(5);
>> figure(h1); % sets the current figure to h1
>> plot([0:0.1:2*pi], sin([0:0.1:2*pi]); % plots a sine wave inside the first figure window
 

Similar threads

Back
Top