sgtitle disappearing when getframe saves frame for animation

3 ビュー (過去 30 日間)
Gavin Donley
Gavin Donley 2022 年 12 月 1 日
コメント済み: Gavin Donley 2022 年 12 月 5 日
Hello,
I am trying to animate a multi-panel figure made using subplot. I need a overal title for the figure, which I have been making with sgtitle. The title shows properly when I run the following code in a live script:
clear,clc,close all
n_rows=501;
A=rand(n_rows,n_rows)
figure(1)
hold on
for c=1:2
subplot(1,2,c)
imshow(A,[0,1])
axis on xy
colorbar
title('subtitle')
xlabel('x label')
ylabel('y label')
end
sgtitle(figure(1),'main title')
hold off
mov_f=getframe(figure(1));
But when I go to look at the resulting 1 frame movie (using the movie command), the title is absent:
I am wondering if it there is a way to keep the main title at the top within the saved frame of the animation? I am currently using R2022b. Any suggestions are welcome. (Note: the code I gave here is an example of the issue I am encountering, the full project has different datasets in each subplot and multiple animation frames.)
  1 件のコメント
Walter Roberson
Walter Roberson 2022 年 12 月 1 日
If it is acceptable to write the image directly into a file in your circumstances, then consider replacing the getframe() with exportgraphics at least experimentally.

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

採用された回答

Benjamin Kraus
Benjamin Kraus 2022 年 12 月 1 日
When I run a slightly modified version of your code, the resulting image includes the "main title". I tested in R2022b.
Here is the code I ran:
n_rows=501;
A=rand(n_rows,n_rows);
f = figure;
for c=1:2
subplot(1,2,c)
imshow(A,[0,1])
axis on xy
colorbar
title('subtitle')
xlabel('x label')
ylabel('y label')
end
sgtitle(f,'main title')
mov_f=getframe(f);
figure
imshow(mov_f.cdata)
I'm not sure why it isn't working for you, but let me suggest something else you can try that might work better: tiledlayout and nexttile.
For example:
n_rows=501;
A=rand(n_rows,n_rows);
f = figure;
t = tiledlayout(1,2);
for c=1:2
nexttile(t)
imshow(A,[0,1])
axis on xy
colorbar
title('subtitle')
xlabel('x label')
ylabel('y label')
end
title(t,'main title')
mov_f=getframe(f);
figure
imshow(mov_f.cdata)
  1 件のコメント
Gavin Donley
Gavin Donley 2022 年 12 月 5 日
Thank you Benjamin, the tiled layout and nexttile seem to work well for me as a solution.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAnimation についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by