0

At first, I would like to say that this is not a duplicate of this: Reading CSV files with MATLAB?

In my script, I am trying to read a CSV file (sensor data). It is of the format:

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

and suddenly I get this error:

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;

This code used to work well without this error and this is the first time I am seeing this. I am not sure if textScan would be of any help. here is my code snippet:

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

Any help or info would be helpful.

1 Answer 1

1

M = csvread(filename) reads a comma-separated value (CSV) formatted file into array M. The file must contain only numeric values.

Your first column is a date, not a numerical value.

M = csvread(filename,R1,C1) reads data from the file starting at row offset R1 and column offset C1.

So you may skip the first column by using rawData = csvread([filePath, '/', fileList.name],0,1);

Also if your version is < 2016b, this MATLAB Answer suggests to use textscan instead, with 'HeaderLines',8 and appropriate format discriptor, 'Delimiter', and if necessary, 'EndOfLine' declarations.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.