Azzera filtri
Azzera filtri

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

3 visualizzazioni (ultimi 30 giorni)
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 Commento
Stephen23
Stephen23 il 18 Dic 2021
Modificato: Stephen23 il 18 Dic 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).

Accedi per commentare.

Risposte (1)

Image Analyst
Image Analyst il 18 Dic 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.

Categorie

Scopri di più su File Operations in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by