sgtitle disappearing when getframe saves frame for animation
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Gavin Donley
am 1 Dez. 2022
Kommentiert: Gavin Donley
am 5 Dez. 2022
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 Kommentar
Walter Roberson
am 1 Dez. 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.
Akzeptierte Antwort
Benjamin Kraus
am 1 Dez. 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)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Animation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



