sgtitle disappearing when getframe saves frame for animation

5 views (last 30 days)
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 Comment
Walter Roberson
Walter Roberson on 1 Dec 2022
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.

Sign in to comment.

Accepted Answer

Benjamin Kraus
Benjamin Kraus on 1 Dec 2022
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 Comment
Gavin Donley
Gavin Donley on 5 Dec 2022
Thank you Benjamin, the tiled layout and nexttile seem to work well for me as a solution.

Sign in to comment.

More Answers (0)

Categories

Find more on Animation in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!