Unexplained error on dir

35 views (last 30 days)
Joseph Smerdon
Joseph Smerdon on 22 Jun 2017
Commented: Walter Roberson on 24 Jun 2017
I'm trying to import data files while skipping headers. This is the entirety of my code:
function[dataCurves,curveFiles,curveFilenames]=loadData(headersize,Average)
curveFiles = dir;
curveFilenames = {curveFiles.name};
numberOfDataFiles=size(curveFilenames,1);
try
for counterCurves= 4:numberOfDataFiles
filecell=curveFilenames(counterCurves,1);
file=filecell{1};
dataCurves(counterCurves-3,:,:)=transpose(dlmread(file,'\t',headersize,0));
end
dataCurves(:,3,:)=[]
end
It quits, saying this:
>> [dataCurves,curveFiles,curveFilenames]=loadData(396,0)
Error in loadData (line 3)
curveFiles = dir;
Output argument "dataCurves" (and maybe others) not assigned during call to
"loadData".
>>
It worked earlier today (not very well, but at least it ran). Now, I can't get it to work, even if I restart MATLAB. If I enter the [dir] command into the MATLAB terminal it executes fine, but it will not execute from inside this function.
Any help gratefully received.
Edit:
After reading some comments and Walter's suggested answer, I removed the 'try' error handler. This caused it to throw an error at the second last line, so I removed that too, which brought it back to the same (empty) error. If I remove the offset to the counter, it throws a different error (about '.' being a directory, so can't be read - which is why I added the offset in the first place.) Any offset causes the original error to be thrown again.
I'm using 2015b on Mac OS X. The number of files in the folder is 240, and they all have the same filename stem, starting with 'i(v)'. I can run dir from the command line, no problem:
>> [dataCurves,curveFiles,curveFilenames]=loadData(396,0)
Error in loadData (line 12)
curveFiles = dir;
Output argument "dataCurves" (and maybe others) not assigned during call to
"loadData".
>> curveFiles = dir;
>>
The cell matrix resulting from this has '.', '..', and '.DS_Store' as the first three values in the first column. Thanks for your suggestions, but if I understand them correctly, they don't fit the symptoms (of my enhanced disclosure in this edited request for help). If I figure it out, I'll leave my own answer.
Edit 2: Reading Edit 1 over, I realised that it wouldn't throw an error at the second last line if it hadn't already managed dlmread, because it wouldn't have gotten there. I tackled this error ('Deletion requires an existing variable.') by dimensioning dataCurves at the beginning. Now the whole thing works. I don't know why this fixed the error with 'dir'.
Edit 3: The above didn't throw an error, but it didn't actually work. The below is what eventually worked, so I'm happy. However, I'm still mystified as to why I got the error in the first place. But thanks all for your help. I didn't actually know that dir took arguments ...
function[dataCurves,curveFiles,curveFilenames,Average]=loadData(headersize);
curveFiles = dir('*(*)*');
curveFilenames = {curveFiles.name};
numberOfDataFiles=size(curveFilenames,2)
dataCurves=zeros(numberOfDataFiles,3,200);
for counterCurves= 1:numberOfDataFiles
filecell=curveFilenames(1,counterCurves);
file=filecell{1};
dataCurves(counterCurves,:,:)=transpose(dlmread(file,'\t',headersize,0));
end
dataCurves(:,3,:)=[];
dataCurves=permute(dataCurves,[3 2 1]);
Average=mean(dataCurves,3);
  9 Comments
Walter Roberson
Walter Roberson on 24 Jun 2017
!touch ! \# \$ % \& \( \) \+ \,
d = dir;
{d.name}
ans =
1×11 cell array
{'!'} {'#'} {'$'} {'%'} {'&'} {'('} {')'} {'+'} {','} {'.'} {'..'}
OS-X El Capitan.
Filenames that begin with '-' or '*' or '"' or "'" also sort before . and ..

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 22 Jun 2017
Either the dlmread of the very first entry attempted failed or the number of entries was less than 4.
Please keep in mind that the order of the entries is not defined by dir(), and reflects whatever the operating system tells MATLAB. In turn, the operating system relies upon whatever the file system subsystem tells it, which can depend upon what kind of file system it is and upon administrator-configurable settings such as whether the file system is case sensitive.
In practice, NTFS appears to return file names sorted by byte value. That is not the same as "." and ".." always being the first two entries: there are a series of other characters that can appear in filenames on NTFS file systems that can sort before "." (I posted a specific list a few weeks ago.)
You should not be counting on dir() returning the files in any particular order. If you want to skip some entries such as "." and ".." then you can remove directories from the list (if that is the criteria) and you can sort names yourself, and it sometimes makes sense to use the natsortorder() File Exchange Contribution.
With regards to individual files: they can be unreadable due to permission problems, or due to being in an unexpected format for dlmread. With older versions of MATLAB (ending roughly R2014a) dlmread cannot handle numeric files that have text in them, even if the text is within the header lines.
Also I recently discovered that at least on OS-X, some of the older routines such as textread() cannot handle names with non-ASCII characters such as µ . That should, however, not affect dlmread() which calls into textscan() rather than textread().
  1 Comment
dpb
dpb on 22 Jun 2017
"...dlmread of the very first entry attempted failed.." good catch, Walter, neglected that can error in the first loop. Probably the most likely case, indeed.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!