フィルターのクリア

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 Center および File ExchangeFile Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by