[Matlab] Making a movie from plots

In summary, the person is having trouble visualizing the trajectory of a spring pendulum model and wants to make a movie of the motion. They have written a code to do so, but it is overwriting their other plots. They are wondering if there is a way to store the movie frames in memory so they can be played separately from the other plots. The expert suggests creating separate scripts for generating the plots and playing the movie, or using the "figure" command to plot to specific windows.
  • #1
Nick89
555
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
  • #2
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
 
  • #3




Yes, it is possible to plot the plots into memory and store them in the F frames array. You can use the "hold on" command before plotting the frames to prevent them from overwriting the previous plots. This will allow you to still have the individual plots while also creating a movie from them. Additionally, you can also adjust the axis limits and formatting of the individual plots to make them more clear and visually appealing. This will provide a better understanding of the motion of the bob in the x-y plane. Good luck with your project!
 

1. How do I save a series of plots as a movie in Matlab?

To save a series of plots as a movie in Matlab, you can use the VideoWriter function. First, create a VideoWriter object by specifying the desired file name and format. Then, use a for loop to iterate through each plot and use the writeVideo function to add each frame to the movie. Finally, use the close function to save and close the movie file.

2. Can I customize the properties of the movie in Matlab?

Yes, you can customize the properties of the movie in Matlab. You can specify the frame rate, quality, and compression settings using the VideoWriter function. Additionally, you can use the set function to modify other properties such as the title, axes, and colorbar of the movie.

3. How can I add annotations or text to the movie in Matlab?

To add annotations or text to the movie in Matlab, you can use the text function within the for loop that iterates through each plot. You can specify the position, font size, and font color of the text. Alternatively, you can use the title or xlabel/ylabel functions to add annotations to the movie.

4. Is it possible to add audio to the movie in Matlab?

Yes, it is possible to add audio to the movie in Matlab. You can use the audiowrite function to create an audio file from a vector of audio data. Then, you can use the AudioVideoWriter function to combine the audio file with the movie. You can also specify the start and end times for the audio to synchronize it with the movie.

5. Can I export the movie from Matlab to a different file format?

Yes, you can export the movie from Matlab to a different file format. The VideoWriter function allows you to specify the desired format using the FileFormat property. You can choose from formats such as AVI, MPEG-4, and GIF. Additionally, you can use the imwrite function to export each frame of the movie as an image file.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
865
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
838
  • Engineering and Comp Sci Homework Help
Replies
2
Views
810
  • Engineering and Comp Sci Homework Help
Replies
1
Views
949
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Calculus and Beyond Homework Help
Replies
10
Views
971
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
873
Back
Top