textscan loop works on windows but not on macOS - why?
Show older comments
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 Comments
Jos (10584)
on 1 Dec 2017
Do the filenames contain special characters or perhaps path separators (different on Mac OS en windows!)?
Ekaterina Avershina
on 1 Dec 2017
Guillaume
on 1 Dec 2017
Two points to make the question clearer:
- The code does not "crash". It errors with invalid file indentifier.
- The error may be reported on the textscan line but has nothing to do with textscan. The error is because fopen failed to open the file.
The most common reason why fopen fails to open a file is because the file path is incorrect.
So, how is the cell array files generated? Are you sure that files do exist?
Jos (10584)
on 1 Dec 2017
Looks fine. What is the error message you get when it crashes?
Ekaterina Avershina
on 1 Dec 2017
Accepted Answer
More Answers (3)
Ekaterina Avershina
on 1 Dec 2017
Edited: Ekaterina Avershina
on 1 Dec 2017
3 Comments
Guillaume
on 1 Dec 2017
The body of the loop is altogether different from what you posted in the question. In particular, any path that contains spaces, you try to open the same path without spaces. I don't know much about macOS but I would assume the two are not equivalent.
Any reasons why you're not using matlab dir?
files = dir(fullfile('.', dirname, '*.txt'));
for file = files'
[fid, errmgs] = fopen(fullfile('.', dirname, file.name));
assert(fid > 0, 'Failed to open "%s" because %s', files{i}, errmsg);
data = textscan(fid, -all-the-formatspecs-);
fclose(fid);
end
I-Lin Wang
on 16 Aug 2022
What's dirname?
Walter Roberson
on 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
on 1 Dec 2017
0 votes
1 Comment
Stephen23
on 1 Dec 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:
Ekaterina Avershina
on 1 Dec 2017
Edited: Ekaterina Avershina
on 1 Dec 2017
0 votes
1 Comment
Ekaterina Avershina
on 1 Dec 2017
Categories
Find more on Scope Variables and Generate Names in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!