MATLAB CSV读取错误

时间:2018-03-15 03:55:10

标签: matlab csv

首先,我想说这与此不重复:Reading CSV files with MATLAB?

在我的脚本中,我正在尝试读取CSV文件(传感器数据)。它的格式为:

2015-10-08 01:00:00.000,-0.762,-0.68,-0.234
2015-10-08 01:00:00.013,-0.762,-0.676,-0.234
2015-10-08 01:00:00.025,-0.762,-0.672,-0.234
2015-10-08 01:00:00.038,-0.762,-0.672,-0.23

突然间我收到了这个错误:

Error using dlmread (line 147)
Mismatch between file and format character vector.
Trouble reading 'Numeric' field from file (row number 1, field number 1) ==> 
 ,-0.02,-0.004,1.004\n

Error in csvread (line 48)
m=dlmread(filename, ',', r, c);

Error in getAvgData (line 17)
    rawData = csvread([filePath, '/', fileList.name]);

 Error in precomputeProcess (line 31)
                getAvgData;

此代码过去运行良好,没有此错误,这是我第一次看到这个。我不确定textScan是否会有任何帮助。这是我的代码片段:

for k = 1:length(hourSampleRateList)
hourSampleRate = hourSampleRateList(k);
disp(['Start at sampling rate: ', num2str(hourSampleRate)]);
for hour = startHour:endHour
    hourStr =  num2str(hour,'%02i');

    filePath = fullfile(pwd, username, 'MasterSynced', yearStr, monthStr,dayStr,hourStr);
    fileList = dir([filePath, '/RawDataFloat*.csv']);
    if (isempty(fileList))
        continue;
    end
    rawData = csvread([filePath, '/', fileList.name]);
    avgData = zeros(ceil(length(rawData)/hourSampleRate), 4);

    j = 1;
    for i = 1:hourSampleRate:length(rawData)-1;
        avgData(j, :) = mean(rawData(i:i+hourSampleRate-1, :));
        j = j + 1;
    end

    filePath = fullfile(pwd, username, 'MasterSynced', yearStr, monthStr,dayStr,hourStr);
    myPath = [filePath, '\Avg', num2str(hourSampleRate, '%03i'), '-', fileList.name];

    if exist(myPath, 'file') == 2
        delete(myPath);
    end
    dlmwrite(myPath,avgData,'delimiter',',','precision','%.3f');
    disp(['Day-Hour(', dayStr, '-', hourStr, '): completed.']);
end
end

任何帮助或信息都会有所帮助。

1 个答案:

答案 0 :(得分:1)

  

M = csvread(filename)读取格式化的逗号分隔值(CSV)   文件到数组M.文件必须只包含数值。

您的第一列是日期,而不是数值。

  

M = csvread(filename,R1,C1)从行开始的文件中读取数据   偏移R1和列偏移C1。

因此,您可以使用rawData = csvread([filePath, '/', fileList.name],0,1);

跳过第一列

此外,如果您的版本是< 2016b,this MATLAB Answer建议改为使用textscan,使用'HeaderLines',8和适当的格式描述符,'Delimiter',以及必要时使用'EndOfLine'声明。