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

조회 수: 4 (최근 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에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by