How do I load ASCII dat file to MATLAB

1 次查看(过去 30 天)
After reading some articles in the forum, I used textscan to load my .dat file to MATLAB. Unfortunately, MATLAB only read the first line of my .dat file.
  • load does not work, most likely because 7th column is double followed by "....."
fid=fopen('newtext.dat');
C=textscan(fid,'%d %d %d %d %d %d %d');
this led to
C=
[775841] [644] [552] [1185] [617] [534] [1153]
Any hints or answers to this problem would be appreciated!
Here's a sample of my .dat file:
775841 643.5 551.6 1185.0 617.3 533.6 1153.0 .....
775843 643.6 552.3 1185.0 621.0 531.8 1153.0 .....
775845 643.6 552.9 1185.0 624.7 530.1 1152.0 .....
775847 643.4 553.4 1185.0 625.6 529.7 1153.0 .....
775849 643.1 553.4 1186.0 622.8 530.7 1154.0 .....
775851 642.8 553.5 1187.0 620.0 531.7 1154.0 .....
775853 642.9 554.0 1187.0 616.6 534.4 1154.0 .....
775855 643.1 554.8 1186.0 613.3 537.3 1154.0 .....
775857 643.5 555.5 1185.0 614.4 538.2 1154.0 .....
775859 643.7 555.5 1184.0 618.7 536.3 1155.0 .....

采纳的回答

dpb
dpb 2015-5-14
编辑:dpb 2015-5-14
fid=fopen('newtext.dat');
fmt=[repmat('%f',1,7) '.....']; % include the junk as pattern matching
C=cell2mat(textscan(fid,fmt,'collectoutput',true));
should give a single array of all the values.
  5 个评论
dpb
dpb 2015-5-14
"...a missing "'" in the repmat call for the fmt."
Dang! What ya' get's for "air code".
Joseph Cheng
Joseph Cheng 2015-5-14
I just wish "air code" was as flashy as "air guitar". I would like to say most of my typos are "purposely" put to test the person but they are usually just "air code" mistakes.

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2015-5-14
编辑:Image Analyst 2015-5-14
Please attach the file with the paper clip icon in case anyone wants to try some code on it to help you. Also, maybe the ..... is causing a problem. Maybe either see if you can get rid of it, or try putting ..... into the format string in textscan(). Also, since you're using floating point numbers, not integers, you'll have to use %f instead of %d.
  1 个评论
Kenny Kim
Kenny Kim 2015-5-14
It seems that I can't upload the file because it is in .dat format. I tried using %f instead of %d. The numbers in C became more accurate (643.5000 instead of 644) which is helpful. Still C is only using first line. I think you are right that the dots at the end are causing the problem but I don't know how to get rid of it quickly. The whole file has about 28000 lines so I can't do it by hand.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by