I had promised "some more love" for that first line... "the one that didn't work". Here it is.
There are some interesting ways to manipulate cell arrays that I didn't know until I researched this answer a bit more... In particular, the use of cellfun()
. I am going to share what I learned.
For example, when you have a cell array of filenames
fnames = {files.name};
you can apply a regex to find files that match a certain criterion (e.g. "doesn't start with a dot")
crit = '^[^.]+';
% return cell array of results, [] if no match
rxResult = regexp( fnames, crit );
% loop over all cells, set true if regex matched
nodot = (cellfun('isempty', rxResult)==0);
% The `nodot` array is a logical array telling us which are "good" files.
totalGoodFiles = sum(nodot);
Now you can use
fileidx = find(~[files.isdir] & nodot);
On the other hand, you are using a for
loop already, so you could put your test in there (edited to include file count):
fCount=0;
for i = 1:length(fileidx)
if( files(fileidx(i)).name(1) ~= '.')
filename = [dirname, '/', files(fileidx(i)).name];
fCount = fCount+ 1;
fid = fopen(filename, 'r');
end
end
As an aside, there is the function fullfile()
which you can use to concatenate directories and file names; it understands the correct file separator to use for your operating system. Quite handy - better than concatenating '/'. When you try to run on a Mac it will fail...
A further aside - the built in constant filesep
will return the correct file path separator to use on your platform; good for making your code portable.