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

3 次查看(过去 30 天)
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!

回答(1 个)

Steven Lord
Steven Lord 2020-1-8
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 个评论
Jo Nelson
Jo Nelson 2020-1-13
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')

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by