Table variable subscripts must be real positive integers

Using the data from https://la.mathworks.com/help/finance/black-litterman-portfolio-optimization.html, and running the code head(T(:,["Dates" benchmarkName assetNames])), get the following error: Table variable subscripts must be real positive integers, logicals, character vectors, or cell arrays of character vectors. I dont now how to fix it.
thanks

9 件のコメント

Dave B
Dave B 2021 年 7 月 29 日
This code (copied from the referenced page) works for me, can you double check what's in the benchmarkName and assetNames variables?
T = readtable('dowPortfolio.xlsx');
assetNames = ["AA", "AIG", "WMT", "MSFT", "BA", "GE", "IBM"];
benchmarkName = "DJI";
head(T(:,["Dates" benchmarkName assetNames]))
Eduardo Orellana
Eduardo Orellana 2021 年 7 月 29 日
in the benchmarkName is just the name DJI and the assetNames the names "AA", "AIG", "WMT", "MSFT", "BA", "GE", "IBM", here is the whole code:
>> T = readtable('dowPortfolio.xlsx');
>> assetNames = ["AA", "AIG", "WMT", "MSFT", "BA", "GE", "IBM"];
benchmarkName = "DJI";
head(T(:,["Dates" benchmarkName assetNames]))
Table variable subscripts must be real positive integers, logicals, character vectors, or cell arrays of character vectors.
Dave B
Dave B 2021 年 7 月 29 日
What version of MATLAB? (i.e. >> version)
The error message doesn't indicate string as an option which makes me think it's before 2016b
In which case, try:
assetNames = {'AA' 'AIG' 'WMT' 'MSFT' 'BA' 'GE' 'IBM'};
benchmarkName = 'DJI';
head(T(:,['Dates' benchmarkName assetNames]))
Eduardo Orellana
Eduardo Orellana 2021 年 7 月 29 日
Version 2018, I tried the 'Dates' but didnt work.
when running the whos code get:
whos benchmarkName
Name Size Bytes Class Attributes
benchmarkName 1x1 134 string
Eduardo Orellana
Eduardo Orellana 2021 年 7 月 29 日
編集済み: Eduardo Orellana 2021 年 7 月 29 日
The data shows in the following format:
'03-01-2006' 10847,4100000000 28,7200000000000 68,4100000000000 51,5300000000000 68,6300000000000
'04-01-2006' 10880,1500000000 28,8900000000000 68,5100000000000 51,0300000000000 69,3400000000000
'05-01-2006' 10882,1500000000 29,1200000000000 68,6000000000000 51,5700000000000 68,5300000000000
I dont know if that is an issue
Dave B
Dave B 2021 年 7 月 29 日
How about breaking down the problem a little:
head(T)
head(T(:,'Dates'))
head(T(:,"Dates"))
head(T(:,benchmarkName))
head(T(:,assetNames))
Which of these lines fails/works?
Eduardo Orellana
Eduardo Orellana 2021 年 7 月 29 日
編集済み: Eduardo Orellana 2021 年 7 月 29 日
The last three didnt work, that is:
head(T(:,"Dates"))
head(T(:,benchmarkName))
head(T(:,assetNames))
Dave B
Dave B 2021 年 7 月 29 日
編集済み: Dave B 2021 年 7 月 29 日
That helps, this sort of fits with my previous hypothesis i.e. that you need to provide char not string:
Let's work our way back to the final bit building off of:
head(T(:,'Dates'))
head(T(:,'DJI')) % just a different table variable
head(T(:,{'Dates' 'DJI'}) % Two table variables
% specifying 'DJI' in a workspace variable
benchmarkName = 'DJI';
head(T(:,{'Dates' benchmarkName}))
% specifying all of the assetNames
assetNames = {'AA' 'AIG' 'WMT' 'MSFT' 'BA' 'GE' 'IBM'};
head(T(:,assetNames))
% one way to put these all together
head(T(:,{'Dates' benchmarkName assetNames{:}}))
% the line of code from my previous comment
% (just in case something got confused and this works now)
head(T(:,['Dates' benchmarkName assetNames]))
Eduardo Orellana
Eduardo Orellana 2021 年 7 月 30 日
It worked. Thank you Dave.

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

回答 (1 件)

Eike Blechschmidt
Eike Blechschmidt 2021 年 7 月 29 日

0 投票

I guess you just forgot the quotation marks around benchmarkName and assetNames.

カテゴリ

ヘルプ センター および File ExchangeTables についてさらに検索

タグ

質問済み:

2021 年 7 月 29 日

コメント済み:

2021 年 7 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by