how to extract data from directory

조회 수: 32 (최근 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 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