how to extract data from directory

36 ビュー (過去 30 日間)
sam moor
sam moor 2017 年 1 月 3 日
コメント済み: Walter Roberson 2017 年 1 月 19 日
I have main directory and 20 sub directories in main directory. In each sub directories there are 4 files. Now, I would like to extract those 4 files from each sub directories in matlab files so that I can do manipulation for each file without doing manually. Is there any suggestion for this?

採用された回答

Walter Roberson
Walter Roberson 2017 年 1 月 4 日
project_dir = pwd(); %or give a particular directory name
mainDirContents = dir(project_dir);
mainDirContents(~[MainDirContents.isdir]) = []; %remove non-folders
mask = ismember( {MainDirContents.name}, {'.', '..'} );
mainDirContents(mask) = []; %remove . and .. folders
num_subfolder = length(mainDirContents);
for subfold_idx= 1 : num_subfolder
this_folder = fullfile( project_dir, mainDirContents(subfold_idx).name );
fprintf('Now working with folder "%s"\n', this_folder );
at2Contents = dir( fullfile(this_folder, '*.AT2') );
num_at2 = length(at2Contents);
for at2idx = 1 : num_at2
this_at2 = fullfile( this_folder, at2Contents(at2idx).name );
now do something with the file whose complete name is given by this_at2
end
end
  7 件のコメント
Walter Roberson
Walter Roberson 2017 年 1 月 19 日
Are your subdirectories numbered 1 to 22? Or do they have the names given? Those names include slashes, and slashes mark directory delimiters, so you cannot be directly using those names.
Walter Roberson
Walter Roberson 2017 年 1 月 19 日
num = xlsread('NormalizationFactors.xls');
norm_factors = num(:,end);
subfolder_results = cell(num_subfolder, 1);
for subfold_idx = 1 : num_subfolder
subfolder_name = mainDirContents(subfold_idx).name;
this_folder = fullfile( project_dir, subfolder_name );
fprintf('Now working with folder "%s"\n', this_folder );
folder_id = sscanf(subfolder_name, '%d', 1);
this_norm_value = norm_factors(folder_id);
at2Contents = dir( fullfile(this_folder, '*.AT2') );
num_at2 = length(at2Contents);
each_file_results = cell(num_at2, 1);
for at2idx = 1 : num_at2
this_at2 = fullfile( this_folder, at2Contents(at2idx).name );
at2_content = ReadAT2File( this_at2 ); %might as well put it inside a function
each_file_results{at2idx} = this_norm_value * at2_content;
end
subfolder_results{subfold_idx} = each_file_results;
end

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

その他の回答 (1 件)

Geoff Hayes
Geoff Hayes 2017 年 1 月 3 日
sam - use dir to get an array of the sub-directories (from the main directory). Then iterate over each element in this array using isdir to ensure that it is a directory. If so, then again use dir on this sub-directory to get an array of files within this sub-directory. Iterate over this array, and perform whatever manipulation that you need for the file depending upon its type. See Data Import and Export for details.
  6 件のコメント
sam moor
sam moor 2017 年 1 月 3 日
Hi Geoff, let say If I only want to import data file having extension .AT2 from each sub folders and run a loop in every subfolders. I used fullfile but gives me error
Geoff Hayes
Geoff Hayes 2017 年 1 月 4 日
sam - as Walter has shown below, use a filter with dir to find all those files that have an extension of AT2.

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

カテゴリ

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