How to skip '.' and '..' directory while looping over files in a folder?

18 views (last 30 days)
Hi I want to loop over files in the folder to process the data. If I start my iteration from 1, it shows the error
Unable to read file 'C:\Users\bdham001\Desktop\Spectra\.'. Input cannot be a directory.
But if I start my iteration from 3, it works but my problem is I am trying to make any array from the values obtained after processing these files and the array will only start filling values from third position.
here is my code
myDir = uigetdir; % gets directory
myFiles = dir(fullfile(myDir));% Added closing parenthese!
val750= zeros(1,400);
for i = 1:5
baseFileName = myFiles(i).name;
fullFileName = fullfile(myDir, baseFileName); % Changed myFolder to myDir
fprintf(1, 'Now reading %s\n', fullFileName);
file= load(fullFileName);
val750(1,i)=sum(file(800,1:end),2);
end

Answers (2)

Image Analyst
Image Analyst on 29 Jul 2020
Try this:
myDir = uigetdir; % Use user to browse to a directory
myFiles = dir(fullfile(myDir))
numFiles = length(myFiles)
val750= zeros(1, numFiles);
for k = 1 : numFiles
if myFiles(k).isdir
% Skip the file if it's a folder.
continue
end
baseFileName = myFiles(k).name;
fullFileName = fullfile(myDir, baseFileName); % Changed myFolder to myDir
fprintf(1, 'Now loading %s\n', fullFileName);
storedStructure = load(fullFileName)
data = storedStructure.yourData; % Needs to be a 2-D array with at least 800 rows.
if size(data, 1) >= 800
val750(1,k) = sum(data(800, :)); % Sum up all elements in row 800
else
warningMessage = sprintf('File %s does not contain 800 rows, it has only %d rows.', size(data, 1));
uiwait(warndlg(warningMessage));
end
end
You will need to adapt it to figure out what's in your .mat files that you're trying to load with the load() function.
  4 Comments
Bibek Dhami
Bibek Dhami on 29 Jul 2020
Actually my file is in unix executable file format which is not allowed to upload so I have attached the excel file for it.
Image Analyst
Image Analyst on 29 Jul 2020
Well how is the load() function supposed to know how to read in your unix executable? As far as I know, load() is for reading in .MAT files (MATLAB's proprietary format), and text files. If you have just some binary file with random stuff it in, load() probably will throw an error. You'd have to use fread() and then open it as a binary file and then parse out the stuff properly because you know the format of what's inside the file.

Sign in to comment.


Steven Lord
Steven Lord on 29 Jul 2020
You probably don't want to filter just '.' and '..', you probably want to filter all directories. You also shouldn't assume that they're the first two entries in the list as package directories whose names start with + precede them in the list.
D = dir(fullfile(matlabroot, 'toolbox', 'matlab', 'general'));
directories = D([D.isdir]);
{directories.name}.' % Display all directory names
directories(1) % This is neither '.' nor '..' in my R2020a installation
directories(3) % This is '.'
directories(4) % This is '..'
files = D(~[D.isdir]);
{files.name}.' % Display all file names
  2 Comments
Bibek Dhami
Bibek Dhami on 29 Jul 2020
Once I checked for the package directories, I found 100 such directories. But how to filter them out?
Steven Lord
Steven Lord on 29 Jul 2020
Note that in my example above, all the directories are in the directories struct array and all the non-directories are in the files struct array. If you wanted to operate only on the files in D, use the files struct array. If you wanted to operate only on the directories that aren't named . or ..:
if isequal(scalarStruct.name, '.') || isequal(scalarStruct.name, '..')
% Skip it
else
% Process it
end
scalarStruct is one element of one of the struct arrays D, directories, or files from my example.

Sign in to comment.

Categories

Find more on Search Path 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!