Chase plot changes figure size internally

2 ビュー (過去 30 日間)
Arpan
Arpan 2025 年 5 月 13 日
コメント済み: Arpan 2025 年 5 月 14 日
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

採用された回答

Walter Roberson
Walter Roberson 2025 年 5 月 13 日
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.
  1 件のコメント
Arpan
Arpan 2025 年 5 月 14 日
Thank you Walter. Very neat.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWeather and Atmospheric Science についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by