how to extract data from directory

35 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 File Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by