Matlab code for accessing different folder data

8 ビュー (過去 30 日間)
Arijeet Sinha
Arijeet Sinha 2018 年 9 月 11 日
回答済み: Jan 2021 年 5 月 4 日
I have 20 excel file of data in 20 different folder. Want to access them all in one matlab code for plot. And can't put all in same folder. Actually all have same name. Need help!!
  2 件のコメント
Stephen23
Stephen23 2018 年 9 月 11 日
編集済み: Stephen23 2018 年 9 月 11 日
@Arijeet Sinha: loop over the folders, read the file data. What have you tried so far?
Arijeet Sinha
Arijeet Sinha 2018 年 9 月 11 日
I use loop to read the file data. It's working. But it's not storing the data. When read the next file, previous file data is loss. I want all the 20 excel file from different folder store in same workspace.

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

回答 (2 件)

Stephen23
Stephen23 2018 年 9 月 11 日
編集済み: Stephen23 2021 年 5 月 4 日
Very similar to the MATLAB documentation shows:
you will want to either use dir or sprint to get the folder names. Something like this should work:
P = 'directory where the 20 subdirectories are';
S = dir(fullfile(P,'*'));
S = S([S.isdir]); % remove files
S = natsortfiles(S); % optional
for k = 1:numel(S)
F = fullfile(P,S(k).name,'filename.txt');
S(k).data = dlmread(F); % or whatever function reads your data.
end
All of your data will be in the structure array S.

Jan
Jan 2021 年 5 月 4 日
Folder = 'C:\Base\Folder\';
FileList = dir(fullfile(Folder, '**', 'YourFile.xlsx'));
Data = cell(1, numel(FileList));
for k = 1:numel(FileList)
File = fullfile(FileList(k).folder, FileList(k).name);
Data{k} = readtable(File); % Or however you import the data
end

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by