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

9 ビュー (過去 30 日間)
Chang seok Ma
Chang seok Ma 2021 年 9 月 7 日
編集済み: Dave B 2021 年 9 月 7 日
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'));
  2 件のコメント
Dave B
Dave B 2021 年 9 月 7 日
Thanks @Jan, somehow I combined the two! (I edited my answer)
Jan
Jan 2021 年 9 月 7 日
Thanks for fixing the code.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by