how to extract data from directory

31 views (last 30 days)
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?

Accepted Answer

Walter Roberson
Walter Roberson on 4 Jan 2017
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 Comments
Walter Roberson
Walter Roberson on 19 Jan 2017
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 on 19 Jan 2017
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

Sign in to comment.

More Answers (1)

Geoff Hayes
Geoff Hayes on 3 Jan 2017
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 Comments
sam moor
sam moor on 3 Jan 2017
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 on 4 Jan 2017
sam - as Walter has shown below, use a filter with dir to find all those files that have an extension of AT2.

Sign in to comment.

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!