フィルターのクリア

Loading files from subfolders, the file name in all the folders are same but with different data.

5 ビュー (過去 30 日間)
Parthiban C
Parthiban C 2017 年 12 月 18 日
回答済み: Image Analyst 2017 年 12 月 18 日
I am trying to combine results from a simulation. The simulation results file contains 40 sub folders and the files in all the folders are same but with different data. Example my first folder contains files file1,file2,file3,....,file 30. Every other sub folders also contain files with same name. Now I need to load the data of file1 from all the folders. Can someone help me out regarding this?
  2 件のコメント
Rik
Rik 2017 年 12 月 18 日
Starting from R2016b, you can use dir with path wildcards, otherwise you will have to use dir to generate the list of folders.

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

回答 (2 件)

Image Analyst
Image Analyst 2017 年 12 月 18 日
See attached demo to process files in a folder and all its subfolders..

Walter Roberson
Walter Roberson 2017 年 12 月 18 日
projectdir = '.'; %name directory that has the subfolders in it
dinfo = dir(projectdir);
dinfo(~[dinfo.isdir]) = []; %remove non-directories
dinfo(ismember({dinfo.name}, {'.', '..'})) = []; %remove . and .. directories
foldernames = fullfile(projectdir, {dinfo.name});
numfolder = length(foldernames);
MaxFile = 30;
data_by_file = cell(MaxFile, 1);
for K = 1 : MaxFile
thisfile = sprintf('file%d', K);
instance_names = fullfile(foldernames, thisfile);
data_for_this_name = [];
for F = 1 : numfolder
data_for_this_instance = LoadInData( instance_names{F} ); %do whatever to load data
data_for_this_name = [data_for_this_name; data_for_this_instance];
end
data_by_file{K} = data_for_this_name;
end
  2 件のコメント
Parthiban C
Parthiban C 2017 年 12 月 18 日
Thanks for your help Walter. I have a question in this. For example if I am searching for file1 in all the sub folders where we are mentioning that?
Walter Roberson
Walter Roberson 2017 年 12 月 18 日
for K = 1 : MaxFile
thisfile = sprintf('file%d', K);
that causes iteration over file1, file2, file3, etc.
The data for file1 will put together and stored into data_by_file{1}, for file2 will be in data_by_file{2} and so on.

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

カテゴリ

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