Chase plot changes figure size internally
4 views (last 30 days)
Show older comments
I am trying to run a driving Scenario in a chase Plot and save the animation in a figure as:
scenario = drivingScenario;
ls = lanespec(2,'Width',5);
roadCenters = [-20 0; 5 0];
road(scenario,roadCenters,'Name','Road 1','Lanes',ls);
roadCenters = [10 5; 10 30];
road(scenario,roadCenters,'Name','Road 2','Lanes',ls);
roadCenters = [10 -5; 10 -30];
road(scenario,roadCenters,'Name','Road 3','Lanes',ls);
roadCenters = [15 0; 40 0];
road(scenario,roadCenters,'Name','Road 2','Lanes',ls);
car1 = vehicle(scenario, 'ClassID', 1, 'Position', [-20, -2.5, 0], 'Length', ...
3, 'Width', 2, 'Height', 1.5);
waypoints = [-20, -2.5; 40, -2.5];
speed = [12 12];
yaw = [0 0];
smoothTrajectory(car1, waypoints, speed, 'Yaw', yaw, 'Jerk', 2);
vidObj2 = VideoWriter('SCP_Chase_Plot','MPEG-4');
open(vidObj2);
fig2 = figure('Name', 'Chase Plot', 'Units', 'pixels', 'Position', [100, 100, 640, 480]);
ax2 = axes('Parent', fig2);
chasePlot(car1,'Centerline','on', 'Parent', ax2);
set(fig2, 'Units', 'pixels', 'Position', [100, 100, 640, 480]);
while advance(scenario)
set(fig2, 'Units', 'pixels', 'Position', [100, 100, 640, 480]);
frame2 = getframe(ax2);
writeVideo(vidObj2, frame2);
pause(0.01)
end
However, when I run the code, it gives me the error:
Error using VideoWriter/writeVideo (line 368)
Frame must be 640 by 480
0 Comments
Accepted Answer
Walter Roberson
on 13 May 2025
frame2 = getframe(ax2);
writeVideo(vidObj2, frame2);
You are fetching graphics data from an axes. It turns out, however, that the size of an axes can vary slightly, mostly due to slight differences in the exact width of tick labels as the tick labels change. But when you go to writeVideo, each frame after the first one must be exactly the same size as the first frame.
A workaround is to use
frame2 = getframe(ax2);
frame2 = imresize(frame2.cdata, [640 480]);
writeVideo(vidObj2, frame2);
If you use this technique, you might notice the exact position of the drawn right boundary moving around a little according to how many pixels beyond the boundary any given final pixel label extends.
More Answers (0)
See Also
Categories
Find more on Weather and Atmospheric Science in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!