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

3 views (last 30 days)
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!

Answers (1)

Steven Lord
Steven Lord on 8 Jan 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 Comment
Jo Nelson
Jo Nelson on 13 Jan 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')

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!