0
0
MATLABdata~10 mins

Animation basics in MATLAB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Animation basics
Initialize figure and plot
Start loop over frames
Update plot data
Refresh display with drawnow
Pause for animation speed
Check if more frames
Repeat
The animation starts by setting up a plot, then repeatedly updates the plot data inside a loop, refreshing the display each time until all frames are shown.
Execution Sample
MATLAB
x = linspace(0,2*pi,100);
fig = figure;
h = plot(x,sin(x));
for k = 1:50
    y = sin(x + k*0.1);
    set(h, 'YData', y);
    drawnow;
    pause(0.05);
end
This code animates a sine wave moving to the right by updating the plot's Y data in a loop.
Execution Table
Stepk (frame)y (sin values)ActionDisplay Update
11sin(x + 0.1)Set YData to yPlot shows sine wave shifted by 0.1
22sin(x + 0.2)Set YData to yPlot updates to sine wave shifted by 0.2
33sin(x + 0.3)Set YData to yPlot updates to sine wave shifted by 0.3
...............
5050sin(x + 5.0)Set YData to yPlot updates to sine wave shifted by 5.0
51EndN/ALoop endsAnimation stops
💡 Loop ends after 50 frames, animation complete
Variable Tracker
VariableStartAfter 1After 2After 3...After 50Final
kN/A123...50End
ysin(x + 0)sin(x + 0.1)sin(x + 0.2)sin(x + 0.3)...sin(x + 5.0)N/A
Key Moments - 3 Insights
Why do we use 'set(h, 'YData', y)' instead of re-plotting inside the loop?
Using 'set' updates the existing plot efficiently without creating a new plot each time, as shown in the execution_table rows where only YData changes but the plot handle 'h' stays the same.
What is the role of 'drawnow' in the animation loop?
'drawnow' forces MATLAB to refresh the figure window immediately after updating YData, so the animation appears smooth, as seen in each step's Display Update.
Why do we include 'pause(0.05)' in the loop?
'pause' slows down the loop so the animation is visible to our eyes, otherwise it would run too fast to see changes, matching the timing between steps in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of k and what action is performed?
Ak=3, re-plot entire graph
Bk=2, set YData to sin(x + 0.2)
Ck=3, set YData to sin(x + 0.3)
Dk=1, set YData to sin(x + 0.1)
💡 Hint
Check row 3 in execution_table for k and Action columns
At which step does the animation loop end according to the execution_table?
AStep 50
BStep 51
CStep 1
DStep 100
💡 Hint
Look at the exit_note and last row in execution_table
If we remove 'pause(0.05)', what would happen to the animation speed?
AAnimation would run faster and might appear jumpy
BAnimation would run slower
CAnimation would stop
DNo change in animation speed
💡 Hint
Refer to key_moments explanation about 'pause' slowing the loop
Concept Snapshot
Animation basics in MATLAB:
- Initialize figure and plot once
- Use a loop to update plot data (e.g., YData)
- Use 'set' to update plot efficiently
- Call 'drawnow' to refresh display
- Use 'pause' to control animation speed
- Loop ends after desired frames
Full Transcript
This animation example in MATLAB starts by creating a figure and plotting a sine wave. Then, inside a loop from 1 to 50, it updates the sine wave by shifting it horizontally. The plot's YData is updated using 'set', and 'drawnow' refreshes the figure so the change is visible. A short pause slows the loop so the animation can be seen clearly. The loop ends after 50 frames, completing the animation.