Borrar filtros
Borrar filtros

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

3 visualizaciones (últimos 30 días)
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 comentario
Stephen23
Stephen23 el 18 de Dic. de 2021
Editada: Stephen23 el 18 de Dic. de 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).

Iniciar sesión para comentar.

Respuestas (1)

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

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by