Title is missing when saving a figure using print, saveas, exportgraphics, and printpreview
15 次查看(过去 30 天)
显示 更早的评论
Context: When plotting images, I have often set the tick marks to face outward, where they are not obscured by the image. This change requires that I nudge the title upward by a small amount so that it is separated from the ticks.
Problem: When I save the file using print, saveas, exportgraphics or printpreview, the title is sometimes missing in the output file. (See attached examples above.)
I found that this problem occurs when the image is plotted using pcolor (and probably surf as well). It does not occur when using plot and imagesc. Annotation is used below to draw an outline of the outer extent of the figure. The nudged title clearly lies within that limit.
Work Around: The problem is fixed by ensuring that the Units property for the title is set to data after adjusting the title position. Note that I have filed a bug report with Matlab.
Versions: This problem did not occur with Matlab versions 2022a and earlier. It started with version 2022b and continues with version 2023a.
Coded example:
function test_figureTitleProblem
%... Simple demonstration of missing title problem.
% When plotting images, I have often set the tick marks to face outward,
% where they are not obscurved by the image. This change requires that
% I nudge the title upward by a small amount so that it is separated
% from the ticks.
%
% Problem: When I save the file using print, saveas, exportgraphics
% and printpreview, the title is missing in the output file.
% I found that this problem occurs when the image is plotted using
% pcolor, and does not occur when imagesc is used. I also found
% that the problem can be fixed by changing the Units property for the title
% back to data (see below). Annotation is used below to draw an outline of
% the outer extent of the figure. The nudged title clearly lies within
% that limit.
%% Initialize variables
%... Load Matlab's global topographic data
load('topo.mat', 'topo');
%... Create grid vectors
lon = 0:359;
lat = -89:90;
%% Plot
figure
%... Missing title occurs with pcolor, and not with imagesc
pcolor(lon, lat, topo)
shading interp
% imagesc(lon, lat, topo)
% axis xy
%... Format plot
hA = gca;
hA.Box = 'on';
hA.TickDir = 'out'; % Oriented tick marks to face outward
hA.Layer = 'top'; % Places grid lines and tick markers on top of image
hA.FontSize = 16;
hA.LineWidth = 1;
%... Render title, and nudge upward to separate from outward tick marks
hT = title('Demonstration of Demonstration of Missing Title', 'FontSize', 16);
hT.Units = 'normalized';
hT.Position(2) = hT.Position(2) + 0.02;
% hT.Units = 'data'; % Uncomment this line to fix problem
%... Draw rectangle to show outer limit for figure
annotation("rectangle", hA.OuterPosition, Color="red", LineWidth=1)
%... Start print preview to show that the title is missing
print('missingTitle_print.pdf', '-dpdf')
saveas(hA, 'missingTitle_saveas.pdf')
exportgraphics(hA, 'missingTitle_exportgraphics.pdf')
printpreview
end
0 个评论
回答(1 个)
Pratheek
2023-3-29
Hi Mark,
I've tried to reproduce the problem you mentioned, but I've been unable to do so on my end. Specifically, the files are correctly saved with appropriate titles when using functions like "print," "saveas," "exportgraphics," or "printpreview."
Using MATLAB R2023a version:
Even in MATLAB R2022b version:
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Printing and Saving 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!