I'm using
while
drawnow;hold off
end
to create an animation. But this animation is just the 6th position as in subplot(2,3,6) (or through next tile repeatedly). To save this animation to the computer while not keeping the other subplots 1~5, what to do?

2 件のコメント

Walter Roberson
Walter Roberson 2024 年 8 月 16 日
??
That is an infinite loop. Inside the infinite loop, you request that the graphics queue be flushed, and you turn off hold . But you do not do anything else -- no drawing at all, no recording of previously drawn material.
It is not clear what you are trying to do?
feynman feynman
feynman feynman 2024 年 11 月 14 日
thanks for your answer. I omitted '%draw something' as in your code

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

 採用された回答

Walter Roberson
Walter Roberson 2024 年 8 月 16 日

1 投票

ax6 = subplot(2,3,6);
count = 0;
while true
%draw something
count = count + 1;
filename = fullfile(SAVEDIRECTORY, "image" + count + ".png");
exportgraphics(ax6, filename);
end

8 件のコメント

feynman feynman
feynman feynman 2024 年 11 月 18 日
編集済み: feynman feynman 2024 年 11 月 18 日
however, does this approach work for .avi? If I have the following:
v = VideoWriter("file.avi");v.FrameRate=5;open(v);
while...
...
frame = getframe(gcf);writeVideo(v,frame);
end
I directly inserted your code but didn't work since File format 'avi' is not valid for export.
Walter Roberson
Walter Roberson 2024 年 11 月 18 日
That looks like it would work.
File format avi is valid for VideoWriter.
You would not have any exportgraphics() in this case.
feynman feynman
feynman feynman 2024 年 11 月 18 日
編集済み: feynman feynman 2024 年 11 月 18 日
good to hear that. Then how does ax6 = subplot(2,3,6) get in without exportgraphics()?
Walter Roberson
Walter Roberson 2024 年 11 月 18 日
You have getframe(gcf) which captures the image of the entire current figure.
Note that you could also getframe(ax6) if you wanted to capture subplot(2,3,6) only.
feynman feynman
feynman feynman 2024 年 11 月 24 日
thanks a lot, almost worked. But there's the following issues:
when
subplot24=subplot(2,2,[2;4]);
...
frame = getframe(subplot24);writeVideo(v,frame);
Error using VideoWriter/writeVideo (line 368)
Frame must be 903 by 1616
when
subplot2=subplot(2,2,2);
...
frame = getframe(subplot2);writeVideo(v,frame);
Error using VideoWriter/writeVideo (line 368)
Frame must be 379 by 677
Walter Roberson
Walter Roberson 2024 年 11 月 24 日
Unfortunately, getframe() results can end up being slightly different sizes. The exact size of axes depends upon the tick labels.
So you need something like
v = VideoWriter("file.avi");v.FrameRate=5;open(v);
first_frame = true;
while...
...
frame = getframe(gcf);
if first_frame
frame_size = size(frame.cdata);
remembered_size = frame_size(1:2);
first_frame = false;
else
frame.cdata = imresize(frame.cdata, remembered_size);
end
writeVideo(v,frame);
end
Walter Roberson
Walter Roberson 2024 年 11 月 24 日
subplot24=subplot(2,2,[2;4]);
%...
subplot2=subplot(2,2,2);
subplot24 is expected to be about twice the size of subplot2 . You will have trouble writing both of them to the same video, unless you imresize() to a common size.
feynman feynman
feynman feynman 2024 年 11 月 25 日
amazing, thanks so much!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeAnimation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by