How do I save a Figure with a Filename based on it's contents?

3 visualizaciones (últimos 30 días)
Jo Nelson
Jo Nelson el 8 de En. de 2020
Comentada: Jo Nelson el 13 de En. de 2020
I've written some code to extract data from an excel spreadsheet into a .mat file for further processing. For each .mat file, I want to produce a corresponding figure showing the data that it contains.
I've managed to automatically name the .mat file using the start and end dates of the data that it contains, and I want to name the figure in the same way. However I get an error message when I use the following code:
FileName = ["DMFlowData", StartDate, EndDate]
Filename = strjoin(FileName)
print (fig, FileName,'-fillpage','-dpdf')
'The messages that I get are:
'Error in checkArgsForHandleToPrint'
Handle input argument contains nonhandle values.
Adding ' ' either side of my variable FileName just results in a figure saved as FileName.pdf, not the dates. I've omitted the code used to create the figure.
Thanks!

Respuestas (1)

Steven Lord
Steven Lord el 8 de En. de 2020
What does the variable named fig contain? I think you intend for it to be the fig input argument listed in the Description section of the print documentation page, but it contains a handle to something that's not a figure object or Simulink block diagram.
Maybe it's an axes handle, in which case you should instead print the figure that contains it? So in the example below you'd print using f (the figure) rather than ax (the axes.)
ax = axes
f = ancestor(ax, 'figure')
  1 comentario
Jo Nelson
Jo Nelson el 13 de En. de 2020
I think it is a normal Figure, as the code works fine with a plain text filename specified (e.g. Thisismyname instead of 'FileName').
Here is the code for it:
fig = gcf;
fig.PaperPositionMode = 'auto';
fig_pos = fig.PaperPosition;
fig.PaperSize = [fig_pos(3) fig_pos(4)];
print (fig, FileName, '-fillpage', '-dpdf')

Iniciar sesión para comentar.

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by