textscan loop works on windows but not on macOS - why?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ekaterina Avershina
am 1 Dez. 2017
Kommentiert: Walter Roberson
am 16 Aug. 2022
Hi all,
I have a list of files that i need to open, schematically the script looks like:
for i=1:length(files)
x=fopen(files{i});
data=textscan(x, -all-the-formatspecs-);
fclose(x)
end
The script works perfectly fine on windows and goes through all the files in the list. When the script is run on macOS, though, an 'invalid file identifier' error message pops up after it's gone through the loop couple of times. Also, there is no consistency on -when- it crashes. sometimes after it's gone through 10 files, sometimes - through 2... If i try to open the file it crashed on, individually using same commands - it works... What can be the problem?
5 Kommentare
Akzeptierte Antwort
Guillaume
am 1 Dez. 2017
Bearbeitet: Guillaume
am 1 Dez. 2017
To find out why fopen failed to open a file, change your script to:
for i=1:length(files)
[fid, errmsg]=fopen(files{i});
assert(fid > 0, 'Failed to open "%s" because %s', files{i}, errmsg);
data = textscan(fid, -all-the-formatspecs-);
fclose(fid)
end
This should give you a better description of the problem.
0 Kommentare
Weitere Antworten (3)
Ekaterina Avershina
am 1 Dez. 2017
Bearbeitet: Ekaterina Avershina
am 1 Dez. 2017
3 Kommentare
Walter Roberson
am 16 Aug. 2022
dirname is a character vector that is being provided by the user; @Ekaterina Avershina coded it at https://www.mathworks.com/matlabcentral/answers/370404-textscan-loop-works-on-windows-but-not-on-macos-why#answer_294131
Ekaterina Avershina
am 1 Dez. 2017
1 Kommentar
Stephen23
am 1 Dez. 2017
Using ls and cellstr to get filenames
files=cellstr(ls(['./' dirname]));
is quite unusual. Standard MATLAB practice is to use dir, and loop over the structure that it returns: this gives the same behavior on all OS's. See the MATLAB documentation:
or FAQs:
Siehe auch
Kategorien
Mehr zu File Operations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!