1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[Matlab] Making a movie from plots

  1. Jun 4, 2009 #1

    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 (Text):
    % movie
    for i = 1:length(X)
        plot(X(i),Y(i),'o'); axis([-10 10 -10 10]);
        F(i) = getframe;
    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: Jun 4, 2009
  2. jcsd
  3. Jun 4, 2009 #2


    User Avatar
    Science Advisor

    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
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook