MATLAB: Read CSV file with column headers and row headers.

csvMATLAB

Hi,
I'd like to know how to read a CSV file that has column headers as well as row headers. Is there a function in Matlab that allows you to do this? Like keeping separately the row headers, the column headers and the data itself? It would be really useful to me, but I don't know if there's a way.
I tried csvread and obviously it didn't work because my csv is not entirely made of numeric data. And I don't know how to use the features in Textscan of column headers and row headers appropriately.
This is how the file looks like.
fid = fopen('FX Returns.csv', 'rt');
data = textscan(fid,'%s %f %f %f','headerlines', 1, 'delimiter', ',');
fclose(fid);
This reads everything except the column headers. The row headers get stored into a cell array, actually each column gets stored into a cell array if the data type specified is string, numerical array if it specified as floating point values.
But I want the data merged into a single matrix, if that's possible. For this case, I'd like to get a 8×3 matrix (for this example), with just the numeric data.
And separately, read only the column headers.
Thanks for reading and for helping out if you can.

Best Answer

Hey guys,
I just went for an even simpler solution, I used this function called csvimport, from here, I can get everything into a single array. After that, I get separetely the row and column headers, and the data separately.
Thanks for your feedback anyways, it was helpful :)