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

15 次查看(过去 30 天)
Hello,
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
try
cd('D:\Dropbox\MPED\data\CEX\output')
path = 'D:\Dropbox\MPED\data\CEX\output';
catch
end
try
cd('C:\Users\Dropbox\MPED\data\CEX\output')
path = 'C:\Users\Dropbox\MPED\data\CEX\output';
catch
end
.
.
.
filename = fullfile('path\figures\', strcat('result','_',specification,'_','intensive') );
print(filename, '-dpng','-r300')
%print(strcat('stimulus','_',specification,'_','intensive'),'-dpng','-r300')
hold off
end

采纳的回答

Dave B
Dave B 2021-9-7
编辑:Dave B 2021-9-7
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;
end
filename = fullfile(fp, 'figures', strcat('result', '_', specification, '_', 'intensive'));

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by