Reading .dat csv like file in Matlab

조회 수: 2 (최근 30일)
asdada
asdada 2016년 10월 23일
답변: asdada 2016년 10월 24일
Hey guys!
I need some help reading a .dat file in MATLAB (as an array).
The content of the file (if I open it with notepad) is like this
"TOA5","CR800Series","CR800","36460","CR800.Std.28","CPU:HAC.CR8","48946","HAC_Meteo_Data_05"
"TIMESTAMP","RECORD","Anonymous1","Anonymous2","Tair_Avg","Tair_Std","RH_Avg","RH_Std","SolRad_Avg","SolRad_Std","AcVirTemp_Avg","AcVirTemp_Std","WV_XYZ_Avg","WV_XYZ_Std","WD_Elev_Avg","WD_Elev_Std","WD_Azim_Avg","WD_Azim_Std","WV_Azim_Avg","WV_Azim_Std","WV_X_Avg","WV_X_Std","WV_Y_Avg","WV_Y_Std","WV_Z_Avg","WV_Z_Std","PTemp","batt_volt_Min"
"TS","RN","","","deg_Celsiou","deg_Celsiou","%","%","W/m2","W/m2","","","","","","","","","","","","","","","","","",""
"","","Smp","Smp","Avg","Std","Avg","Std","Avg","Std","Avg","Std","Avg","Std","Avg","Std","Avg","Std","Avg","Std","Avg","Std","Avg","Std","Avg","Std","Smp","Min"
"2016-05-21 04:05:00",9825,105,711,3.93,0.014,100,0,"NAN","NAN","NAN","NAN","NAN","NAN","NAN","NAN","NAN","NAN","NAN","NAN","NAN","NAN","NAN","NAN","NAN","NAN",23.01,13.87
"2016-05-21 04:10:00",9826,105,711,3.934,0.018,100,0,"NAN","NAN","NAN","NAN","NAN","NAN","NAN","NAN","NAN","NAN","NAN","NAN","NAN","NAN","NAN","NAN","NAN","NAN",23.01,13.87
There are many more lines underneath and I don't really need the first few ones. All I need is to have the numbers in their cells. When I use Excel or Libre Office to open the file, it opens without the commas or the quotes etc.
What would be the best way to read the file? I found some way, but I am missing something to make it work properly :
f= fopen('x.dat');
data=fread(f);
fclose(f);
y = importdata('x.csv') %I converted my .dat to a .csv and that worked well, but I really need it to read from the .dat file
fIDF=fopen(nameFile);
NephtDataAr0= textscan(fIDF, '%s %f %f %f %f %f %f %f %f %f %f %f %s %s','Delimiter',',','CommentStyle','[');
fclose(fIDF); %Someone that has written a program to read some similar data from a .txt file, used the line above. Could that be of any use?
filename = 'csvlist.dat';
M = csvread(filename) %I found this one but I am getting this error
Error using dlmread (line 147)
Mismatch between file and format string.
Trouble reading 'Numeric' field from file (row number 1, field number 1) ==>
"TOA5","CR800Series","CR800","36460","CR800.Std.28","CPU:HAC.CR8","48946","HAC_Meteo_Data_05"\n
Error in csvread (line 48)
m=dlmread(filename, ',', r, c);
Error in readfile2 (line 2)
M = csvread(filename)
Thanks a lot for your time!

채택된 답변

Walter Roberson
Walter Roberson 2016년 10월 23일
It is never possible to use csvread() to read files that have strings in the data. It has only been possible in the last two years or so to use csvread() to read data that has strings in the header.
You should try using readtable() . If that does not work for you then you should switch to using textscan()
numcol = 28;
fmt = ['%q', repmat('%f', 1, numcol-1)];
datacell = textscan(fid, fmt, 'HeaderLines', 4, 'TreatAsEmpty', '"NAN"', 'Delimiter', ',')

추가 답변 (1개)

asdada
asdada 2016년 10월 24일
Wow! Thanks a lot Walter! It worked perfectly!

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by