필터 지우기
필터 지우기

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

조회 수: 3 (최근 30일)
Wesso
Wesso 2021년 12월 18일
답변: Image Analyst 2021년 12월 18일
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
Stephen23
Stephen23 2021년 12월 18일
편집: Stephen23 2021년 12월 18일
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).

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2021년 12월 18일
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.

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by