sgtitle disappearing when getframe saves frame for animation

5 visualizzazioni (ultimi 30 giorni)
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 Commento
Walter Roberson
Walter Roberson il 1 Dic 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.

Accedi per commentare.

Risposta accettata

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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Animation in Help Center e File Exchange

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by