Azzera filtri
Azzera filtri

How do I load ASCII dat file to MATLAB

1 visualizzazione (ultimi 30 giorni)
Kenny Kim
Kenny Kim il 14 Mag 2015
Commentato: Joseph Cheng il 14 Mag 2015
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 .....

Risposta accettata

dpb
dpb il 14 Mag 2015
Modificato: dpb il 14 Mag 2015
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 Commenti
dpb
dpb il 14 Mag 2015
"...a missing "'" in the repmat call for the fmt."
Dang! What ya' get's for "air code".
Joseph Cheng
Joseph Cheng il 14 Mag 2015
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.

Accedi per commentare.

Più risposte (1)

Image Analyst
Image Analyst il 14 Mag 2015
Modificato: Image Analyst il 14 Mag 2015
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 Commento
Kenny Kim
Kenny Kim il 14 Mag 2015
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.

Accedi per commentare.

Categorie

Scopri di più su Data Import and Export in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by