Clear Filters
Clear Filters

fopen error too many argument

1 view (last 30 days)
Mini Me
Mini Me on 10 May 2014
Commented: Mini Me on 11 Sep 2014
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?
  1 Comment
dpb
dpb on 11 May 2014
Edited: dpb on 11 May 2014
We've hammered on the structure of reading in a loop but guess haven't really (directly) addressed the question as posed...
The doc for fopen says in part ...FID is a scalar MATLAB integer valued double which resolves the question of too many inputs--it is not vectorized to open more than a single file in one call so you must use a loop structure of some sort. Using dir and the counted loop is certainly about the most straightforward way there is.
Your memory issues are something totally independent--you're either not reusing memory when you process each file and go on to the next, or are trying to process more files at a single time than your machine has memory available.
There is, I presume, some OS limitation on number of open file handles. DOS used to be otoh 20 or so unless you requested more at startup; I don't know otomh what that might be under various Windows releases but it's sizable I'm sure.
Either of these problems means you need to rethink your approach. Describing what you're trying to accomplish might help in getting suggestions.

Sign in to comment.

Accepted Answer

dpb
dpb on 11 May 2014
Edited: dpb on 11 May 2014
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.

More Answers (1)

dpb
dpb on 10 May 2014
Edited: dpb on 10 May 2014
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')
Depends on what you're trying to do. To process each file in sequence obviously you open each file name in sequence as you're doing. What you then needs must do is to read the full file and do whatever it is you want to do with that data before going on to the next (presuming you're not needing to do something with multiple files at the same time).
Your difficulty here is you've told fread to only read one value instead of the whole file. One would presume what you mean to do would be sotoo...
d=dir(filedirpathnameincludingarestrictivewildcard); % only get matching files
for k=1:length(d)
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
...
  1 Comment
Mini Me
Mini Me on 11 May 2014
Edited: Mini Me on 11 May 2014
Thanks for the update @dpb I am in fact reading the whole file later on. But my question to you is when I read the whole dir te subdir get included also so when I fopen each file in filedir. It tries to open the subdir also which creates an error saying the file is not in the dir. And of course that still wouldn't change the memory issue. What happen is when it's tryna read and close 4000 files in a dir eventually it throws the error saying one of the files doesn't exist in the dir

Sign in to comment.

Categories

Find more on File Operations in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!