MATLAB Answers

Ajpaezm
0

Read CSV file with column headers and row headers.

Ajpaezm
さんによって質問されました 2017 年 11 月 2 日
最新アクティビティ Walter Roberson
さんによって コメントされました 2019 年 7 月 2 日
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 8x3 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.

  2 件のコメント

Rupali Wagh 2019 年 7 月 2 日
i have same problem , the data i want to arrenged with headers, give me the solution
Walter Roberson
2019 年 7 月 2 日
? That file appears to be just 51 numeric columns. I do not see any headers.

サインイン to comment.

タグ

3 件の回答

Ajpaezm
回答者: Ajpaezm
2017 年 11 月 3 日
 採用された回答

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 :)

  0 件のコメント

サインイン to comment.


KSSV
回答者: KSSV
2017 年 11 月 2 日

Use xlsread. Read about it.

  0 件のコメント

サインイン to comment.


Walter Roberson
回答者: Walter Roberson
2017 年 11 月 2 日

If you have R2013b or later, you could use readtable(), ReadRowNames true (not default).
You can either let with ReadVariableNames default to true, in which case you should not add Headerlines option; or you can set ReadVariableNames to false (not default) and add 'HeaderLines', 1 . The difference is that in the first case it will mangle the dates in the header into something that is a valid variable name, such as x3x29x2017; in the second case it will use variables 'var1', 'var2', 'var3' (unless you tell it which variable names to use.)
... If you do not care what the headers are or what the row labels are, and truly want only the numeric entries, then it is possible to use readtable(), but you could instead just use xlsread() with a single output and it will automatically trim out the header and the row name (unless one of the row names accidentally comes out as numeric...)

  0 件のコメント

サインイン to comment.



Translated by