简体   繁体   中英

MATLAB csvread: Specifying a range with only numeric data but adjacent cells on the right contain text

I've got a csv that is formatted in the following manner:

Sample Number           Date/Time           CHANNEL0 CHANNEL1 CHANNEL2 CHANNEL3 Events
1              11/26/2014 04:10:21.3349 PM  0.4643   -2.3232  -3.7053  -1.78    DAQ Start
2              11/26/2014 04:10:21.3350 PM  0.5489   -2.3226  -3.7047  -1.7836  
3              11/26/2014 04:10:21.3351 PM  0.6316   -2.3217  -3.7035  -1.7833  
4              11/26/2014 04:10:21.3352 PM  0.7137   -2.3211  -3.7023  -1.7827  
.
.
.
35675          11/26/2014 04:10:21.3349 PM  0.4643   -2.3232  -3.7053  -1.78    DAQ Stop

I want to read this data into Matlab in one shot. I am trying to use csv read with the following range:

date = csvread('file.csv', 1, 2, [1, 2, 35674, 5])

But every time I try to do this I get the error:

Error using dlmread (line 138)
Mismatch between file and format string.
Trouble reading number from file (row 1u, field 7u) ==> DAQ Start\n
Error in csvread (line 49)
    m=dlmread(filename, ',', r, c, rng); 

(Note: There are some other headers in the file but these are irrelevant, the problem is this "DAQ Start" box)

I don't understand why csvread is having an issue with the text that it outside of it's specified range on the right side. Does specifying a range not actually cause it to ONLY read that range?

I can add that if I increase the row offset by one, so I don't read the row with the DAQ text, and only read to the second last row, then it works. Whenever I try to read either line with this extra text it gets upset. I could put together something with textscan, but I don't think this is going to be as clean because I would need to read the first and last lines separately. Any way to make this work with csvread?

edit: The way that I included the data is lacking commas, I know that. But rest assured, it's a csv.

You can use the importdata function and cut out the unimportant columns:

imported = importdata ('file.csv', ' ', 1)
data = imported.data
parts = data(:,[1:3 5:8])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM