I'm working on some MATLAB code that parses a .csv file into a table. The CVS file has seven columns separated by commas and also contains thousands of rows.
Right now the parsing code looks like this:
fid = fopen('data.csv', 'r');
parsed = textscan(fid, '%s %f %f %f %f %f %f', 'Delimiter',',', 'HeaderLines', 1);
But disp(parsed);
returns an object that contains an array that has only one column. This data looks something like this:
[209,1] = 3/8/2015 16:12:00
[210,1] = 8.09
[211,1] = 952
[212,1] = 603
[213,1] = 100.8
[214,1] = 20.8
[215,1] = 11.3
[216,1] = 10.66
[217,1] = 3/8/2015 16:47:00
[218,1] = 8.1
[219,1] = 950
[220,1] = 604
[221,1] = 100
[222,1] = 15.8
[223,1] = 11.18
[224,1] = 10.71
[225,1] = 3/8/2015 17:22:00
[226,1] = 8.07
[227,1] = 981
How do I break this data up into a table. I want to be able to interface by data like: parsed[1][3]
. I think I'm just missing an obvious parameter to pass into the textscan()
function, but I can't find any documentation on this anywhere.
Any help you could provide would be greatly appreciated!
Update:
Here's a small sample of the CSV file I am working with:
dt,temp,ldo,turbidity,ldo.per,orp,conductivity,ph
3/8/2015 00:02:00,7.99,11.52,3.8,96.3,612,1038,8.01
3/8/2015 00:07:00,7.98,11.52,3.5,96.3,612,1038,8.01
3/8/2015 00:12:00,7.96,11.52,3.4,96.3,612,1038,8.01
3/8/2015 00:17:00,7.97,11.54,3.7,96.5,612,1038,8.01
I'd like the output to look something like:
A =
3/8/2015 00:02:00 7.99 11.52 3.8 96.3 612 1038 8.01
3/8/2015 00:07:00 7.98 11.52 3.5 96.3 612 1038 8.01
3/8/2015 00:12:00 7.96 11.52 3.4 96.3 612 1038 8.01
3/8/2015 00:17:00 7.97 11.54 3.7 96.5 612 1038 8.01
You can use importdata
:
d = importdata('filename.csv');
This gives a struct d
with fields data
(numeric) and textdata
(cell array of strings):
>> d.data
ans =
1.0e+003 *
0.0080 0.0115 0.0038 0.0963 0.6120 1.0380 0.0080
0.0080 0.0115 0.0035 0.0963 0.6120 1.0380 0.0080
0.0080 0.0115 0.0034 0.0963 0.6120 1.0380 0.0080
0.0080 0.0115 0.0037 0.0965 0.6120 1.0380 0.0080
>> d.textdata
ans =
'dt' 'temp' 'ldo' 'turbidity' 'ldo.per' 'orp' 'conductivity' 'ph'
'3/8/2015 00:02:00' '' '' '' '' '' '' ''
'3/8/2015 00:07:00' '' '' '' '' '' '' ''
'3/8/2015 00:12:00' '' '' '' '' '' '' ''
'3/8/2015 00:17:00' '' '' '' '' '' '' ''
To combine them you need a cell array:
result = [d.textdata(2:end,1) num2cell(d.data)];
which gives
result =
'3/8/2015 00:02:00' [7.9900] [11.5200] [3.8000] [96.3000] [612] [1038] [8.0100]
'3/8/2015 00:07:00' [7.9800] [11.5200] [3.5000] [96.3000] [612] [1038] [8.0100]
'3/8/2015 00:12:00' [7.9600] [11.5200] [3.4000] [96.3000] [612] [1038] [8.0100]
'3/8/2015 00:17:00' [7.9700] [11.5400] [3.7000] [96.5000] [612] [1038] [8.0100]