Creating automatically a folder name and cd the folder based on excel file name

2 Ansichten (letzte 30 Tage)
Hi, I have excel names based on countries. for example I have Iraq.xlsx file save under C:\Documents\MATLAB so, I write :
T = readtable('Iraq.xlsx') ;
is it possible when i read the file of iraq to assign automatically a folder name called iraq and cd it in C:\Documents\MATLAB\Iraq. in other words any further computation will be saved in the folder iraq
so if i write for example:
uniquecities=unique(T.city);
save uniquecities
it will be save automatically in the folder iraq
  1 Kommentar
Stephen23
Stephen23 am 18 Dez. 2021
Bearbeitet: Stephen23 am 18 Dez. 2021
Of course, you can MKDIR a new directory.
But using CD is slow and make debugging more complex. It is NOT required to change directories just to import/export data files: using absolute/relative filenames is a much better approach (you will need to use FULLFILE).

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Image Analyst
Image Analyst am 18 Dez. 2021
Try something like (untested):
T = readtable('Iraq.xlsx') ; % Read file into table variable.
% Create folders for every city.
for row = 1 : height(T)
thisCity = T.city{row}; % Get the ciry stored in this row of the table.
folder = fullfile(pwd, thisCity); % Or any other folder instead of pwd.
if ~isfolder(folder) % If the folder doesn't already exist, create it.
fprintf('Creating new folder %s.\n', folder);
mkdir(folder);
end
end
And like Stephen said, don't use cd. Why not? See the FAQ:
Attach your workbook if you need more help.

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by