Clear Filters
Clear Filters

How can I save the figures in the subfolder of current directory path?

11 views (last 30 days)
I want to save the figures in the subfolder of current directory.
The thing is that I am running my code in two different computers and these two have different directory path.
I want to save the figures in the subfolder called figures which is under the folder 'output' so that the path should be 'D:\Dropbox\MPED\data\CEX\output\figures' or 'C:\Users\Dropbox\MPED\data\CEX\output\figures'
I tried as following but it doesn't work...
Is there anyway I can fix this?
% plot all multipliers
clear all
close all
path = 'D:\Dropbox\MPED\data\CEX\output';
path = 'C:\Users\Dropbox\MPED\data\CEX\output';
filename = fullfile('path\figures\', strcat('result','_',specification,'_','intensive') );
print(filename, '-dpng','-r300')
hold off

Accepted Answer

Dave B
Dave B on 7 Sep 2021
Edited: Dave B on 7 Sep 2021
Your code is failing because you're pasting in the string 'path' instead of the variable path.
I wouldn't recommend using try + catch + cd to verify that the folder exists, instead, use exist.
I also wouldn't recommend calling the variable path, particularly if this is a script and not a function (because path is a common and useful built in MATLAB function and naming a variable the same thing will 'shadow' the function).
fp1 = 'D:\Dropbox\MPED\data\CEX\output';
fp2 = 'C:\Users\Dropbox\MPED\data\CEX\output';
if isfolder(fp1) % alternatively, exist(fp1, 'dir') for releases before R2017b
fp = fp1;
else % consider elseif(isfolder(fp2)) followed by an else branch. What should your script do if for some reason neither folder exists?
fp = fp2;
filename = fullfile(fp, 'figures', strcat('result', '_', specification, '_', 'intensive'));

More Answers (0)


Find more on Environment and Settings 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!