fopen error too many argument

조회 수: 2 (최근 30일)
Mini Me
Mini Me 2014년 5월 10일
댓글: Mini Me 2014년 9월 11일
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
dpb
dpb 2014년 5월 11일
편집: dpb 2014년 5월 11일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

dpb
dpb 2014년 5월 11일
편집: dpb 2014년 5월 11일
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.
  댓글 수: 1
Mini Me
Mini Me 2014년 9월 11일
unfortunately I am still having issues read binary files inside my directory: see link: http://www.mathworks.com/matlabcentral/answers/154411-read-all-files-inside-more-than-one-directory

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

dpb
dpb 2014년 5월 10일
편집: dpb 2014년 5월 10일
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
Mini Me
Mini Me 2014년 5월 11일
편집: Mini Me 2014년 5월 11일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by