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

5 vues (au cours des 30 derniers jours)
Chang seok Ma
Chang seok Ma le 7 Sep 2021
Modifié(e) : Dave B le 7 Sep 2021
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

Réponse acceptée

Dave B
Dave B le 7 Sep 2021
Modifié(e) : Dave B le 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;
end
filename = fullfile(fp, 'figures', strcat('result', '_', specification, '_', 'intensive'));
  2 commentaires
Dave B
Dave B le 7 Sep 2021
Thanks @Jan, somehow I combined the two! (I edited my answer)
Jan
Jan le 7 Sep 2021
Thanks for fixing the code.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by