Hi, this is my file and it is called «1.dat»:
bou=200
bou=30480
bou=22860
bou=0
bou=497
number xfe fee prefue cefee ewee wef wfe wefn
1 13767 11061 0 208963921855920 74822197 208963921855920 0 900
2 13767 11061 0 208963949100942 74822207 208963949100942 450 890
3 13767 11061 0 208963981822687 74822217 208963981822687 450 860
4 13757 11051 0 208964003640637 74822227 208964003640637 390 840
i want to ignore the 6 first lines and i used:
D = cell2mat(textscan('1.dat', '%f%f%f%f%f%f%f%f%f', 'headerlines', 6))
but it keeps on returning me «empty matrix: 0-by-6» i've also tried putting 7 instead of 6 (because of the empty line but still doesnt work, same error)
what is wrong?

 採用された回答

dpb
dpb 2016 年 5 月 12 日
編集済み: dpb 2016 年 5 月 12 日

0 投票

textscan uses fopen/fclose to open/close the file and the subsequent file handle, NOT the actual file name. By using the name of the file in the location for the file handle, it is trying to read the string '1.dat' as the data stream which obviously doesn't match the expected data format.
fid=fopen('1.dat','r'); % check for success in real life...
D=cell2mat(textscan(fid, '', 'headerlines', 7)); % if the blank record is there, must count it...
fid=fclose(fid); % close the file after reading...
% do whatever following...
...
NB:
You'll note I used an empty string for the format string -- this has the effect of textscan will internally scan the input record and ascertain the number of fields and automatically return the shape of the resulting cell array to match the file structure. This saves a lot of effort in building format strings and is especially useful if the number of columns isn't known a priori.

2 件のコメント

Van Ly
Van Ly 2016 年 5 月 12 日
it worked! thank you very much!
Jeremy Hughes
Jeremy Hughes 2017 年 7 月 6 日
You could also try READTABLE, it should be able to do most of this automatically in recent releases.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCell Arrays についてさらに検索

質問済み:

2016 年 5 月 12 日

コメント済み:

2017 年 7 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by