How to load a csv file as a datamatrix in matlab?
Date: 2022-12-12 13:33:34
I try to load a csv file in matlab to use a certain column as a vector for a OLS estimation. However, my csv looks like:
Date KCFSI
13 2004-02-01 -0.67
14 2004-03-01 -0.58
15 2004-04-01 -0.57
16 2004-05-01 -0.49
17 2004-06-01 -0.67
...
and I want to have the the column KCFSI
as a vector.
I tried:
x=fopen('kcfsi.csv'); kcfsi=x(:,2);
But I don't even get a matrix for my x. Just get as value : "14" for whatever reason. I want to have something like "2x100"
Answers:
csvread
cannot open csv files containing non-Numeric values as stated in the documentation.
The file must contain only numeric values.
So you should use textscan
as explained in this answer : https://stackoverflow.com/a/19613301/11756186
Alternatively you can use the readtable
built-in function
csvtable = readtable('kcfsi.csv'); kcfsi_array = csvtable.KCFSI; %Column vector with the content of the KCFSI column