csv2table
Creates a table from the .csv file f.
Syntax
t = csv2table(f)
t = csv2table(f, ..., row, col)
t = csv2table(f, ..., dataRange)
t = csv2table(f, ..., 'readlabels', readlabelsvalue)
Inputs
- f
- File name or file handle opened by using fopen.
- delim
- Optional delimiter between values read. If no delimiter is specified, it will be determined from the file f.
- row
- Optional input that specifies the one-based row in the file f from where the data is read.
- col
- Optional input that specifies the one-based column in the file f from where the data is read.
- dataRange
- Optional input that specifies the range of data read. The range could be specified as a 4-element vector in the form [r1, c1, r2, c2]. r1 and c1 are the start row/column the data is read from. r2 and c2 are the end row/column the data is read from. r1, c1, r2, and c2 are zero-based. Range can also be specified as a spreadsheet-style string such as 'A1..B5' or 'A1:B5', where A refers to column 1 of the data and 1 refers to the one-based start row of the data to be read.
- readlabelsvalue
- Optional input which specifies if column labels are to be read from the file. Valid values are off (default) and on.
Outputs
- t
- Table object read from the file f.
Examples
Default options, reading a .csv file with scalar and complex data:
filename = 'csv2table2.csv';
m = rand(5,6);
m(1,1) = 2+i;
csvwrite(filename,m);
t = csv2table(filename)
t = 5x6 table
Column1 Column2 Column3 Column4 Column5 Column6
__________ __________ __________ __________ __________ __________
2 + 1i 0.85794562 0.64589412 0.05671298 0.79172503 0.39278479
0.59284462 0.54488318 0.38438171 0.96366276 0.81216873 0.92559663
0.71518937 0.84725174 0.43758721 0.27265629 0.52889492 0.83607877
0.84426574 0.4236548 0.29753461 0.38344152 0.47997717 0.07103606
0.60276337 0.6235637 0.891773 0.47766511 0.56804456 0.33739616
Read labels from .csv file:
filename = 'table2csv1.csv';
Lake = {'Superior','Michigan','Huron','Erie','Ontario'};
Area = [31700, 22300, 23000, 9910, 7340];
Volume = [2900, 1180, 850, 116, 393];
t = table(Lake, Area, Volume);
table2csv(filename, t)
t2 = csv2table(filename, 'readlabels', 'on')
t2 = 5x3 table
Lake Area Volume
__________ _____ ______
'Superior' 31700 2900
'Michigan' 22300 1180
'Huron' 23000 850
'Erie' 9910 116
'Ontario' 7340 393
Read a .csv file with a data range:
filename = 'table2csv1.csv';
Lake = {'Superior','Michigan','Huron','Erie','Ontario'};
Area = [31700, 22300, 23000, 9910, 7340];
Volume = [2900, 1180, 850, 116, 393];
t = table(Lake, Area, Volume);
table2csv(filename, t)
t3 = csv2table(filename, 'readlabels', 'off', 'B2:C3')
t3 = 2x2 table
Column1 Column2
_______ _______
31700 2900
22300 1180