How do I save a figure to a different directory with a specific figure name?

3 views (last 30 days)
I am trying to save a figure with the type and dateTime variables included in the savefile name. My code uses the input variable " type = 'Triangle' " in order to run, and it saves the start time as a dateTime variable. I want the "type" variable to save based on it's input so that the saved figure will include any string assigned to "type." I am also trying to save the figure to a different directory.
I have a code that uses the variable " type = 'Triangle' " when running the code and saves the time I start running the code as f. I want to save that string variable with the date & time as part of the figure name. I'm trying to also save this figure to a different directory that my code is not in.
This is what I have so far but I am getting the following error: H must be an array of handles to valid figures.
type = 'Triangle';
folderName = 'C:\my\directory';
savefig(fullfile(folderName,[datestr(startTime) type]));

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 28 Feb 2022
savefig(FILENAME) saves the current figure to a file named FILENAME
savefig(H, FILENAME) saves the figures identified by the graphics
handle array H to a MATLAB figure file called FILENAME.
To be robust, specify H and make sure your file name is valid.
H=figure
plot(1:10)
savefig(H, 'myFigure')

More Answers (0)

Categories

Find more on Printing and Saving in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by