Reading multiple csv file from multiple folder

I have a main folder contains 6 sub folder. Each sub folder contains 15 csv file. How to read all this data together?

 採用された回答

Stephen23
Stephen23 2023 年 7 月 13 日
編集済み: Stephen23 2023 年 7 月 14 日

0 投票

P = 'absolute or relative path to the main folder';
S = dir(fullfile(P,'*','*.csv'));
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
T = readtable(F); % or READCELL or READMATRIX
% do whatever with your table/cell/matrix
% e.g. store the data from every iteration:
S(k).data = T;
end

3 件のコメント

Nour Ahmed
Nour Ahmed 2023 年 7 月 13 日
Thank you for your time.
It only save the last csv file. I need to save the data of each subfolder together.
Stephen23
Stephen23 2023 年 7 月 14 日
"I need to save the data of each subfolder together."
You can easily store the imported file data in the structure S:
P = 'absolute or relative path to the main folder';
S = dir(fullfile(P,'*','*.csv'));
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
T = readtable(F); % or READCELL or READMATRIX
S(k).data = T;
end
For example, the 2nd file:
S(2).folder % path
S(2).name % filename
S(2).data % imported data
Nour Ahmed
Nour Ahmed 2023 年 7 月 15 日
It works with me. Thank you for your time.

サインインしてコメントする。

その他の回答 (0 件)

製品

リリース

R2021a

タグ

質問済み:

2023 年 7 月 13 日

コメント済み:

2023 年 7 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by