fopen error too many argument
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commento
dpb
il 11 Mag 2014
Modificato: dpb
il 11 Mag 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.
Risposta accettata
dpb
il 11 Mag 2014
Modificato: dpb
il 11 Mag 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.
Più risposte (1)
dpb
il 10 Mag 2014
Modificato: dpb
il 10 Mag 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
...
Vedere anche
Categorie
Scopri di più su File Operations in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!