textscan loop works on windows but not on macOS - why?

2 ビュー (過去 30 日間)
Ekaterina Avershina
Ekaterina Avershina 2017 年 12 月 1 日
コメント済み: Walter Roberson 2022 年 8 月 16 日
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 件のコメント
Jos (10584)
Jos (10584) 2017 年 12 月 1 日
Looks fine. What is the error message you get when it crashes?
Ekaterina Avershina
Ekaterina Avershina 2017 年 12 月 1 日
Jos, thanks! As Guillaume pointed out, it sends an error of 'invalid file identifier'

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2017 年 12 月 1 日
編集済み: Guillaume 2017 年 12 月 1 日
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.

その他の回答 (3 件)

Ekaterina Avershina
Ekaterina Avershina 2017 年 12 月 1 日
編集済み: Ekaterina Avershina 2017 年 12 月 1 日
Guillaume, thank you for the corrections and the suggestion. (a) yes, i do know that the file exist. When i open the file individually, it works (x gets assigned to 3 or larger). and it goes through to the data; it's only when i use the loop, that the problem occurs. (b) i will try your approach and send a feedback. (c) the filename is given as follows:
files=cellstr(ls(['./' dirname]));
for i=1:length(files)
if contains(files{i},'.txt')
x=fopen(['./' dirname '/' strrep(files{i},' ','')]);
data=textscan(x, -all-the-formatspecs-);
fclose(x)
end
end
  3 件のコメント
I-Lin Wang
I-Lin Wang 2022 年 8 月 16 日
What's dirname?
Walter Roberson
Walter Roberson 2022 年 8 月 16 日

サインインしてコメントする。


Ekaterina Avershina
Ekaterina Avershina 2017 年 12 月 1 日
Thank you for the hint, Guillaume! Without it i would have never guessed what is causing the problem! So fopen fails due to 'too long file name', and that happens because cellstr(ls(dir)) command saves filenames as (num_files*1) cellmatrix on Windows and as 1*1 cell with a 1-row string of all the concatenated filenames on mac!. I don't have a direct access to the mac (it's in another city, and the user sends me print screens of the errors), so each time i assumed it was going through number of files and the last one - is the one it failed on; and it never even slightly occurred to me that the whole list is the 'name' of the file... Thanks a lot!!!
  1 件のコメント
Stephen23
Stephen23 2017 年 12 月 1 日
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
Ekaterina Avershina 2017 年 12 月 1 日
編集済み: Ekaterina Avershina 2017 年 12 月 1 日
Guillaume and Stephen, the only reason i used 'ls' and not 'dir' was that this was the command i found when looking in help for listing the files... Thank you! I will change it to the 'dir' now. As for removing the spaces, yes, i did it deliberately since the 'ls' command returns the list as a char and if the name of one file is shorter than of the other, the spaces are added to it so that all rows (i.e. filenames) have the same length (that is, on windows at least).
  1 件のコメント
Ekaterina Avershina
Ekaterina Avershina 2017 年 12 月 1 日
...and you were right! use of dir saved it and now it works on both! Thanks a lot for all the help! :)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by