how to extract data from directory
Show older comments
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
More Answers (1)
Geoff Hayes
on 3 Jan 2017
1 vote
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
Geoff Hayes
on 3 Jan 2017
What have you tried? From the main directory, use dir to grab the list of directories and files. Something like
mainDirContents = dir;
Now iterate over each element in this array and check to see if it is a directory
for k=1:length(mainDirContents)
if mainDirContents(k).isdir
fprintf('dir name: %s\n',mainDirContents(k).name);
end
end
Depending upon your OS, you may have a couple of invalid directories that you will need to avoid. On my Mac, I would need to do
for k=1:length(mainDirContents)
if mainDirContents(k).isdir && ~strcmpi(mainDirContents(k).name,'.') && ~strcmpi(mainDirContents(k).name,'..')
fprintf('dir name: %s\n',mainDirContents(k).name);
end
end
Now, you mention in your question that you are looking for four files but in your comment you are looking for two files. Which is it? Looking at your other question, there seems to be a bunch of files of which you are only interested in RSN953_NORTHR_MUL009.AT2 and RSN953_NORTHR_MUL279.AT2. So create the path to these files and do something with them
for k=1:length(mainDirContents)
if mainDirContents(k).isdir && ~strcmpi(mainDirContents(k).name,'.') && ~strcmpi(mainDirContents(k).name,'..')
% get the path to the first file
pathToFileA = fullfile(pwd,mainDirContents(k).name,'RSN953_NORTHR_MUL009.AT2');
fprintf('path to file A %s\n',pathToFileA);
% get the path to the second file
pathToFileB = fullfile(pwd,mainDirContents(k).name,'RSN953_NORTHR_MUL279.AT2');
fprintf('path to file B %s\n',pathToFileB);
end
end
What you want to do with the file is up to you...
sam moor
on 3 Jan 2017
Walter Roberson
on 3 Jan 2017
if mainDirContents(k).isdir && ~strcmpi(mainDirContents(k).name,'.') && ~strcmpi(mainDirContents(k).name,'..')
can be simplified to
if ~ismember(mainDirContents(k).name, {'.', '..'})
This code is valid for all operating systems that MATLAB is currently supported on (it might not work if you are still stuck using the VMS version of MATLAB, but it would work for every other operating system that MATLAB has ever be implemented on.)
sam moor
on 3 Jan 2017
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.
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!