Using print function to save figures with text labels

I have often saved multi-part figures using the print function, but recently labels added to these with the text function are no longer being saved in the final image (example below). I explored other options including exportgraphics and saveas, however none have the functionality of 'print'. The same disappearing-text issue is present independent of figure extension (.png, .pdf, etc.). What would be the best way to get the saved image, in this case testfigure.png, to look like the Matlab figure window?
figure(1);clf;
x=linspace(0,2*pi,10);plot(sin(x),'linewidth',2);
t=text(0.8,0.9,'my function','units','normalized'); %label appears in figure window
print('testfigure.png','-dpng','-r300');set(t,'fontweight','bold'); %label is missing in .png file

2 comentarios

I explored other options including exportgraphics and saveas, however none have the functionality of 'print'.
The functionality you're struggling to find in other functions is critical information for us. Otherwise we can't know what to recommend. You can forget about getting print() to work the way you want. It's notoriously bad.
Colin
Colin el 6 de Sept. de 2023
Yes, print() is definitely annoying but up until now has otherwise been working well enough for me in a few well-polished scripts. I guess the functionality I'm really hoping for is:
  • being able to set custom figure dimensions and resolutions
  • having the saved image match the figure window in terms of fonts, colors, subpanel placement, and added text
Sometimes in a pinch I've resorted to taking screenshots of the figure window, which hit all of these points except that their resolution is suboptimal (plus of course a programmatic solution is always better).

Iniciar sesión para comentar.

 Respuesta aceptada

Matt J
Matt J el 6 de Sept. de 2023
Editada: Matt J el 6 de Sept. de 2023

11 comentarios

Thanks for the suggestion. Export_fig sounds very promising from the documentation but upon testing it out, with the last line of the above code modified to be:
export_fig testfigure.png -nocrop;
The saved image again is missing the desired text, and I don't see an obvious option that would instruct export_fig to preserve it.
Matt J
Matt J el 6 de Sept. de 2023
I suggest attaching the original figure in a .fig file, so that we can test the behavior of different solutions directly.
Good suggestion, here is the desired output as a .fig.
I=openfig('desiredoutput.fig','visible');
Hmmm. Here is the image that,
export_fig testfigure.png -nocrop;
gives me. The text is there, as you can see. Maybe try re-downloading export_fig directly from its Github site.
Colin
Colin el 6 de Sept. de 2023
I just downloaded it for the first time yesterday. Could be an incompatibility with R2023a, perhaps?
Matt J
Matt J el 6 de Sept. de 2023
Editada: Matt J el 6 de Sept. de 2023
No, I generated the image above in R2023a. Is it possible you are looking at the wrong image file by accident?
Hmm, I downloaded the package again just to check and reran this line
export_fig anotherattempt.png -nocrop;
with a distinct file name. Unfortunately I still got the same problem as before. Maybe there's a setting that's different? Most of them are default but a few I have changed over time. This is on a 2021 MacBook Pro.
Matt J
Matt J el 7 de Sept. de 2023
I have no experience using it on a Mac. You might try posting a message on the discussion page for export_fig to see if its user community recognizes the problem.
Colin
Colin el 8 de Sept. de 2023
Ok, thank you. I'll do that and will report back here if a solution is found.
Colin
Colin el 8 de Sept. de 2023
Editada: Colin el 8 de Sept. de 2023
As described here
the current workaround solution with export_fig() is to only call text with 'units' as 'data' (which is the default) rather than 'units','normalized'. Then export_fig() will work exactly as expected.
Matt J
Matt J el 8 de Sept. de 2023
Good! Hope Yair fixes it at some point.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.

Productos

Versión

R2023a

Preguntada:

el 6 de Sept. de 2023

Comentada:

el 8 de Sept. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by