MATLAB: Does the figure title not show up in certain figures in MATLAB

appearareacroppedfigureinvisiblelongMATLABmissingnotoutsideshowstringtitle

I was generating a sequence of plots on an axes, and I realized that the graph title does not show up.
How can I make my plots display the title? I used the TITLE function (after generating the axes and plots) and passed in my title string, but nothing happens.

Best Answer

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:
% Create some dummy data to plot
x = linspace(1,10,100);
y = rand(1,100);
% Get handle to the figure and plot the data
h=figure(1);
plot(x,y);
% Get handle to the title. The title is intentionally too long to fit on
% the default figure window.
ht=title('This is a long and big title to test for the title not appearing on the saved file',...
'FontSize',20);
% Get the title Extent. This shows whether the title is on the figure.
titleExtent = get(ht,'Extent')
% Try to save the image. This shows that the title does not fully appear in
% the saved file.
saveas(h,'Test1','jpg')
% Set the units of the figure to normalized. Where bottom left corner of
% the screen corresponds to Position of (0,0)
set(h,'units','normalized')
% Get the position of the figure. Move the figure down towards the bottom
% left corner of the screen.
Position = get(h,'Position');
set(h,'Position',[0.1 0.1 Position(3) Position(4)])
% Perform a loop while the title is not fully in the figure window
while titleExtent(1)<0
Position = get(h,'Position');
% Resize the figure. Increase the figure by 5%
set(h,'Position',[Position(1) Position(2) 1.05*Position(3) 1.05*Position(4)])
% Force update of the plot.
drawnow;
% Get the new title Extent and check if the title is fully on the plot.
titleExtent = get(ht,'Extent')
end
% Set the PaperPositionMode to auto so saveas or print captures what is
% shown on the screen.
set(h,'PaperPositionMode','auto')
% Do two different methods of saving the figure.
print(h,'-djpeg','-r600','Test2')
saveas(h,'Test3','jpg')