MATLAB: Fopen error too many argument

directoryerrorfopenfor loopMATLAB

Here's how my code start filedir=dir(dirpath)
For k=3:length(filedir) % I start the loop from 3 to avoid the subdir . and ..
fid=fopen(filedir(k).name,'rb','ieee-be')
test=fread(fid, 1, 'uint32')
End
Now I know that eventually opening the file inside loop will create memory issue and eventually will crash.. but when I try to open it outside: fid=fopen(filedir.name) it just gives me an error saying too many argument..please help.is there a better way to open outside the loop?

Best Answer

If the file is returned by dir then it does exist unless you're trying to process the directory entries as well as the files.
That's why I suggested using a wildcard in the filename so that only files (not directories) will be included.
If there isn't any filename wildcard that would work (seems unlikely) then use the .isdir field to skip non-files...
d=dir(dirpath);
for k=1:length(d)
if d(k).isdir, continue, end % skip any that are directory entries

fid=fopen(d(k).name,'rb','ieee-be')
data=fread(fid, inf, 'uint32'); % read the full file
fid=fclose(fid); % close the file when done
% do whatever need to do w/ the data here
...
ADDENDUM
d=dir(dirpath);
for k=1:length(d)
if d(k).isdir, continue, end % skip any that are directory entries
...
Or, of course, if you need to traverse subdirectories as well, you can do that inside this loop by nesting or rearrange the order to ensure process all pertinent subdirectories in order first.