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 CenterFile Exchange에서 Animation에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by