In this article we are going to discuss various methods for creating a video in MATLAB. We also present a short tutorial how to create animation in MATLAB, with examples and the original codes. As a reader, you can change the codes to create your own animations in MATLAB.
By Ahmad Atash Afzon, Cademix Institute of Technology
What is an Animation, and Why do we need it?
Animation is useful for many because it allows us to tell stories and communicate emotions and ideas in a unique, easy-to-perceive manner that both young children and adults can understand. In ways that writing and live-action films cannot, animation has helped to connect people all over the world.
An animation is a sequence of still images that are display one after the other. The brain interprets these images as continuous fluid motion when they are showing in rapid succession. The animation is made in the same way as a flip-flop book is made. A flip-flop book is a booklet that displays a series of images that change from one page to the next.
When you scroll through the pages quickly, the graphics appear to animate, as if they are moving or changing in some way. Animation offers numerous benefits and is commonly employed in the fields of science and engineering. It helps in bringing ideas to life or providing context for them.
We’ll look at how to make an animation with Matlab in this article. We’ll also look at the numerous phases needed and how to simplify the activity using Matlab’s built-in functions.
Requirements – What the industry expects from us?
Several industries in Europe use Matlab as an all-purpose framework for programming data analysis, visualization, and post-processing. The requirements vary by business, but the most of industries require basic knowledge and practical expertise with visualization and data processing. One of the most important aspects of the visualization technique is the creation of animations in Matlab. It contains animations and many types of data visualization connected to physical illustrations, as well as displaying various types of plots necessary in a certain Industry.
Matlab professionals should understand how to read data, highlight data, and visualize data. Furthermore, it is critical for the stakeholder that the Matlab expert communicate the results vocally, for example, in a meeting or a conference. Especially with the present progress of social media, the use of animations and new visually appealing graphs and graphics has become critical in attracting a public audience. In summary, it is also a part of the marketing and sales department, and it is extremely important as a dissemination tool for spreading the word and increasing the social influence.
If you want to use this article and if you want to use our codes, you need to have a few items on your promises. First you need to have the Matlab installed on your PC. you can alternatively use the open-source software Octave. We assume that you know the basics of programming in Matlab, therefore we do not describe the fundamentals and easy basics of the Matlab programming.
The workflow for creating an animation in Matlab is very similar to creating a flipbook. Basically we need to have a plot or a graph, and after a specific pause we need to redraw and plot the new one on top of the previous one. For instance the major steps for creating a simple animation in Matlab includes the following steps:
- Run a simulation or generate data.
- Draw/render the scenario at some time t_k.
- Take a snapshot of the scenario.
- Advance time t_k to t_(k+1).
- Saving the movie.
We should consider that some of the above steps for example the steps from number 2 to 4 might repeat sequentially in a loop. We might also use the possibility to do the simulations and do the analysis before going into the plotting part.
Required Functions to create animation in Matlab
In the first step you need to have the possibility to load the data from the data of files. The data files are usually in any special formats that you have decided to use to store your simulation results. In case your simulation results are not very complicated, then you might have the possibility to run a real-time simulation in parallel to the plotting and animations. However, in many cases it is highly recommended that you run the simulations first, store the results in a data file, and then later on try to read the data files and plot of the simulation results. Basic Matlab functions to do this task include read, load, etc.
In the Second Step you need to draw an empty frame for the animation. This may include a simple plot as a placeholder.
In the Third Step We want to grab that frame and save it into the large flipbook when you’ve already drawn one page of the flipbook. This can be performed by Matlab’s get frame function.
The fourth step includes the step is automatically handle if we put the task inside a for loop or a while loop. You may have a lot of data if you are simulating data that is very frequently timely spaced.
You may not want to plot every specific point of your data because the final movie will be extremely thick. So we’ll use the continue function to create the logic of skipping some data.
In the last step in order to save the movie, we will use videowriter and writevideo functions on MATLAB.
For Loops to Create animations in Matlab
You may use a loop and drawnow command to create an animated plot and see it being created in real-time. On each callback, the drawnow command refreshes the figures. To create an animated plot, use it within a loop to plot one variable in one iteration and refresh the figure with the drawnow command.
There are several examples and tasks you can use to create an animated plot. For instance, you can plot a sine wave, and use the animation to change the phase.
t = 1:0.001:2;
x = sin(2pit);
axis([1 2 -1 1])
An Exercise to create animation in Matlab
Active students usually search for project ideas to prove their skills. So, if you understood the basics of animation in Matlab, Then you can easily go through any of the following week projects and choose one of them. these projects are relatively easy projects, and we estimate that an average of students can do any of these projects within a few hours. please feel free to do these projects and consider them as a homework. we will be very happy to guide you and evaluate your projects in this direction. especially for those of you who are in the Cademix carrier autopilot program, you can submit your project to the author or to your mentor, and get further support on it.
Following exercises is about to create animation in Matlab
here is the list of some projects, which is also interesting from an industrial perspective.
Load a CAD file into Matlab, and try to move the parts in for muffin animation.
Load robots model into Matlab, and try to show the basic movements considering the joints and boundary conditions apply light on to the robot. please pay attention that this project can be relatively complicated so will recommend you to pick her up a simple robot such as a robot arm.
Create a fractal design and combine it with an animation. this can be categorized as a combination of a graphic design in Matlab where you can show your skills in the area of animation.
Gather the information about the covid-19 pandemic and the distribution it had in various countries. then try to load this information into Matlab and make an animation on the distribution of the covid-19 cases around the world and show them as an animation in Matlab.
Pick up an existing pathfinding algorithm from the math work website for computing a shortest path between an initial node to a final node. And create an animation to show the item goes exactly through the path.
About the author
Ahmad Atash Afzon is a graduate of electrical engineering from Iran, currently a member of Cademix Career Autopilot Program. He has years of experience in the field of electrical protection system and distribution power of oil industry. Ahmad has more than 9 years of experience working in oil Refineries, Design of Protection systems, power Distribution panels and control circuits of electromotors. He has also extensive knowledge of Matlab programming, including visualization techniques and how to Create animation in Matlab. Please feel free to contact him, if you have any inquiries a question about the topic.