Why does copyobj() fail to copy over title fontsizes (and also colorbars and colormaps)?

24 visualizaciones (últimos 30 días)
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?
h1=figure;
ax(1)=subplot(1,2,1);
plot(1:5,'rx:'); axis square
title('A','FontSize',15);
ax(2)=subplot(1,2,2);
plot(rand(1,5),'sb--'); axis square
title('B','FontSize',15);
h2=figure;
for i=1:2
copyobj(ax(i),h2);
end
  4 comentarios
Matt J
Matt J el 29 de Dic. de 2021
Editada: Matt J el 29 de Dic. de 2021
To add a further example, colormaps and colorbars also don't copy over gracefully:
h1=figure;
for i=1:2
ax(i)=subplot(1,2,i);
imshow(rand(100));
colormap hot
colorbar
title(char('A'+(i-1)),'FontSize',20);
end
h2=figure;
for i=1:2
copyobj(ax(i),h2);
end

Iniciar sesión para comentar.

Respuesta aceptada

Benjamin Kraus
Benjamin Kraus el 21 de Jun. de 2022
Matt,
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
ans = 16.5000
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;
title('A','FontSize',15);
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.
  3 comentarios
Matt J
Matt J el 30 de Jun. de 2022
Editada: Matt J el 30 de Jun. de 2022
Thanks, Ben. But it makes me wonder (and I believe Yair also asked this at the online MAB) why colorbars are figure children rather than axes children (or tiledchartlayout children)? It seems natural that if you copy an axes, you would want the colorbar to go with it.
Benjamin Kraus
Benjamin Kraus el 1 de Jul. de 2022
Editada: Benjamin Kraus el 1 de Jul. de 2022
"why colorbars are figure children rather than axes children": That's a good question. I'm not sure why that design was chosen back when it was decided (it long predates my time at MathWorks), but if I had to guess it is due in large part to the fact that prior to R2014b colorbars and legends were just axes in disguise, and there was no functionality that allowed you to make an axes the parent of another axes.
I also agree that the colorbars (and legends) should be children of the axes. If there were an easy way to change that design, I would.
However, when you put an axes + colorbar into a TiledChartLayout, the colorbar is a child of the TiledChartLayout. Basically, in all scenarios currently, axes and colorbars (and legends) are siblings that share a parent, whether that parent is a figure, panel, or TiledChartLayout.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Red en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by