[Matlab] Making a movie from plots

Click For Summary
SUMMARY

This discussion focuses on creating a movie from plots in MATLAB for a spring pendulum model. The user successfully generates a movie showing the bob's motion but encounters issues with overwriting existing plots. The solution involves using separate scripts for generating plots and movies, and utilizing figure handles to manage multiple plot windows without losing previous visualizations.

PREREQUISITES
  • Familiarity with MATLAB programming and syntax
  • Understanding of plotting functions in MATLAB
  • Knowledge of figure handles in MATLAB
  • Basic concepts of differential equations and their graphical representation
NEXT STEPS
  • Learn how to use MATLAB figure handles for managing multiple plots
  • Explore MATLAB's getframe function for capturing frames in animations
  • Investigate MATLAB's movie function for playback of captured frames
  • Study best practices for separating code functionality into different scripts in MATLAB
USEFUL FOR

MATLAB users, educators, and students working on dynamic simulations, particularly those involved in physics or engineering projects requiring visual representation of motion.

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

  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K