Add changing title to superimposed images while writing GIF

6 views (last 30 days)
Hello, I am making a code to make GIF files out of a series of superimpositions of images but I'm not able to add a title that changes for every frame of the GIF. The code works perfectly for making GIFs with a colorbar, but no way I can add the title. Could someone help me? Thank you!
Here's the code:
figure4=figure(4)
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
M4=max((max(reshape(BBIO,503*647,146))'));
% Setting transparency conditions: if the pixel signal is > 0, then in matrix A it is marked as 1 (transparent). Otherwise it is 0 (opaque)
R4=reshape(BIO,503*647,146);
X=15;
for j=1:146;
for i=1:503*647;
if R4(i,j)<X;
R4(i,j)=0;
else R4(i,j)=1;
end
end
end
A4=reshape(R4,503,647,146);
% Generating images and saving them into GIF
for i=1:146
title(['\lambda=',sprintf('%d',2*i+678),'nm'])
%%% I TRIED THIS BUT GIVES ME ONLY THE VALUE FOR i=1 %%%
ax1 = axes('Parent',figure4);
ax2 = axes('Parent',figure4);
clims=[X M4];
i1=imagesc(BIO(:,:,i),'Parent',ax2,clims);
colormap(ax2,jet);
hold on;
set(i1,'alphadata',A4(:,:,i));
colorbar;
c3=colorbar;
w=c3.Position;
c3.Position=[0.860954412414402,0.110072689511942,0.030111111111111,0.815160955347871]
set(c3,'YTick',[])
hold on;
imagesc(BIO_US_double(:,:,i),'Parent',ax1);
colormap(ax1,gray);
daspect(ax1,[1 1 1]);
daspect(ax2,[1 1 1]);
set(ax1,'Visible','off');
set(ax2,'Visible','off');
hold off;
% gif utilities
drawnow;
frame = getframe(4);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
outfile = 'BIO.gif';
% On the first loop, create the file. In subsequent loops, append.
if i==1
imwrite(imind,cm,outfile,'gif','DelayTime',0,'loopcount',inf);
else
imwrite(imind,cm,outfile,'gif','DelayTime',0,'writemode','append');
end
end
clear('ax1','ax2','clims','i','j','lambda','M4','R4','A4','figure4','i1');

Accepted Answer

Subhadeep Koley
Subhadeep Koley on 6 Nov 2019
Hi, it is difficult to provide exact solution without your variable BBIO and BIO. But I have made some changes in your code to make it behave the way you like. You should assign the changing title to ax1 without setting its visibility to ‘off’. You can refer to the code below.
figure4=figure(4);
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
M4=max((max(reshape(BBIO,503*647,146))'));
% Setting transparency conditions: if the pixel signal is > 0, then in matrix A it is marked as 1 (transparent). Otherwise it is 0 (opaque)
R4=reshape(BIO,503*647,146);
X=15;
for j=1:146
for i=1:503*647
if R4(i,j)<X
R4(i,j)=0;
else
R4(i,j)=1;
end
end
end
A4=reshape(R4,503,647,146);
% Generating images and saving them into GIF
for i=1:146
ax1 = axes('Parent',figure4);
ax2 = axes('Parent',figure4);
clims=[X M4];
i1=imagesc(BIO(:,:,i),'Parent',ax2,clims);
colormap(ax2,jet);
hold on;
set(i1,'alphadata',A4(:,:,i));
colorbar;
c3=colorbar;
w=c3.Position;
c3.Position=[0.860954412414402,0.110072689511942,0.030111111111111,0.815160955347871];
set(c3,'YTick',[])
hold on;
imagesc(BIO_US_double(:,:,i),'Parent',ax1);
colormap(ax1,gray);
daspect(ax1,[1 1 1]);
daspect(ax2,[1 1 1]);
% Set only ax2 visibility off not ax1
set(ax2,'Visible','off');
ax1.XTick = [];
ax1.YTick = [];
% Add your changing title to ax1
title(ax1,['\lambda=',sprintf('%d',2*i+678),'nm']);
hold off;
% gif utilities
drawnow;
frame = getframe(4);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
outfile = 'BIO.gif';
% On the first loop, create the file. In subsequent loops, append.
if i==1
imwrite(imind,cm,outfile,'gif','DelayTime',0,'loopcount',inf);
else
imwrite(imind,cm,outfile,'gif','DelayTime',0,'writemode','append');
end
% Clearing the title for every last frame except for the last one
if 146~=i
title(ax1,'');
end
end
clear('ax1','ax2','clims','i','j','lambda','M4','R4','A4','figure4','i1');
Hope this helps!

More Answers (0)

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!