Title is missing when saving a figure using print, saveas, exportgraphics, and printpreview

8 Ansichten (letzte 30 Tage)
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

Antworten (1)

Pratheek
Pratheek am 29 Mär. 2023
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:
  1 Kommentar
Mark Brandon
Mark Brandon am 2 Apr. 2023
Thank you for your consideration. Your runs above show the figure, but not the pdf files created by "print," "saveas," "exportgraphics," or "printpreview.". I edited my original message to include the pdf files I generated on my Macbook Pro using "print," "saveas," and "exportgraphics". The title is missing from all of those files. The directory on the left shows that your runs generated pdf files. The relevant question is if the title is missing from your pdf files?
Best, Mark

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Printing and Saving finden Sie in Help Center und File Exchange

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by