How do I specify variable names in Matlab?

1 回表示 (過去 30 日間)
Calum Crichton
Calum Crichton 2016 年 1 月 27 日
コメント済み: Walter Roberson 2016 年 1 月 27 日
I have a portfolio of 10 stocks on an excel spreadsheet and have the names of the stocks in the first row. I can import this to Matlab by defining my data as a numeric matrix as opposed to a column vector. This allows me to calculate the variance-covariance matrix, which I was unable to do by importing the spreadsheet as a column vector.
The problem now is that I now cannot specify that my first row is variable names. Does anyone have any advice? I would be most grateful for your help. Thank you!
  1 件のコメント
jgg
jgg 2016 年 1 月 27 日
編集済み: jgg 2016 年 1 月 27 日
Try using readtable instead of the method you're using to import the data. You can then use table2array to calculate your covariance matrix.
Basically, you'll save a lot of time and effort by using the right datatypes off the bat.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2016 年 1 月 27 日
[num, txt] = xlsread('YourXLSFile.xls');
Then the numbers are in num and the variable names are in txt.
  2 件のコメント
Calum Crichton
Calum Crichton 2016 年 1 月 27 日
編集済み: Calum Crichton 2016 年 1 月 27 日
This worked. Thank you! The last thing I need help with is that after doing this how do I define an AssetList? E.g. when I use
p = Portfolio('AssetList', {'stockA','stockB'etc},'RiskFreeRate',CashRet);
it will not recognise the names of the assets
Walter Roberson
Walter Roberson 2016 年 1 月 27 日
p = Portfolio('AssetList', txt(:).', 'RiskFreeRate', CashRet);
I do not know for sure that the cell array needs to be a row vector, but that is what I see in the examples.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Import from MATLAB についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by