Why does copyobj() fail to copy over title fontsizes (and also colorbars and colormaps)?
13 views (last 30 days)
I am attempting to copy a set of subplot axes from one figure to another with copyobj. Everything seems to copy over quite nicely except for the title FontSizes, as illustrated in the example below. Does anyone know why?
plot(1:5,'rx:'); axis square
plot(rand(1,5),'sb--'); axis square
Benjamin Kraus on 21 Jun 2022
The issue with copyobj and the FontSize on axes is a known bug. The issue relates to there being two sources of truth: the FontSize on the axes and the FontSize on the title. When you set the FontSize on the axes, MATLAB force that change down to the Title, whether you've manually specified the FontSize on the title or not.
ax = axes;
ax.Title.String = 'Title';
ax.Title.FontSize = 30;
ax.FontSize = 15; % This resets the FontSize on the Title.
ax.Title.FontSize % This has been updated to reflect the new FontSize * TitleFontSizeMultiplier
The issue with copyobj is the sequence in which MATLAB is (internally) copying properties from the old axes to the new axes. MATLAB copies the title's FontSize before we copy the axes FontSize, so when the axes FontSize value is copied it replaces the value copied from the old title to the new title.
Unfortunately, I don't have any good suggestions for workarounds, except to separately copy the text object (not a great workaround, but it works):
h1 = figure;
ax = axes;
h2 = figure;
ax2 = copyobj(ax,h2);
h3 = figure;
ax3 = copyobj(ax,h3);
ax3.Title = copyobj(ax.Title,ax3); % Separately copying the text object after copying the axes will preserve title properties.