It is possible that the title string is too long to be displayed on the given figure, if the figure width is not wide enough. Below are two possible workarounds:
1) Break up the title string into multiple lines, using the cell array syntax, such as:
title({'line1', 'line2', 'line3'});
2) Programmatically resize the figure until the title fully appears in the window. This example is for a title that is too wide for the plot window. It does not consider a title that does not fit vertically:
x = linspace(1,10,100);
y = rand(1,100);
h=figure(1);
plot(x,y);
ht=title('This is a long and big title to test for the title not appearing on the saved file',...
'FontSize',20);
titleExtent = get(ht,'Extent')
saveas(h,'Test1','jpg')
set(h,'units','normalized')
Position = get(h,'Position');
set(h,'Position',[0.1 0.1 Position(3) Position(4)])
while titleExtent(1)<0
Position = get(h,'Position');
set(h,'Position',[Position(1) Position(2) 1.05*Position(3) 1.05*Position(4)])
drawnow;
titleExtent = get(ht,'Extent')
end
set(h,'PaperPositionMode','auto')
print(h,'-djpeg','-r600','Test2')
saveas(h,'Test3','jpg')
Best Answer