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?

 Akzeptierte Antwort

dpb
dpb am 12 Mai 2016
Bearbeitet: dpb am 12 Mai 2016

0 Stimmen

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 Kommentare

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Gefragt:

am 12 Mai 2016

Kommentiert:

am 6 Jul. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by