フィルターのクリア

Number of variables dependent on user input

1 回表示 (過去 30 日間)
P_Alpha
P_Alpha 2015 年 10 月 12 日
コメント済み: P_Alpha 2015 年 10 月 13 日
Hi Everyone, I would like to have user input for number of Securities and weights for each security in a portfolio. For Example:
numsec = ('Enter the number of securities in Portfolio: ');
numsec = input(numsec)
If the user input is "2" then
Security1 = ('Enter Security 1 Ticker: ');
Security1 = input(Security1,'s');
Security1Wt = ('Enter Security 1 Weight (ex: .25): ');
Security1Wt = input(Security1Wt);
Security2 = ('Enter Security 2 Ticker: ');
Security2 = input(Security2,'s');
Security2Wt = ('Enter Security 2 Weight (ex: .25): ');
Security2Wt = input(Security2Wt);
I want this to be dynamic so that there are Securities and Security weight variables for the number of securities in the portfolio. Sorry if I am not asking this clearly as I am a matlab rookie. Also, if any of the code I posted is ugly and can be done in a betterfashion, please let me know! Kind regards.

採用された回答

the cyclist
the cyclist 2015 年 10 月 12 日
編集済み: the cyclist 2015 年 10 月 12 日
Use cell arrays:
numsec = ('Enter the number of securities in Portfolio: ');
numsec = input(numsec)
Security = cell(numsec,1);
SecurityWt = nan(numsec,1);
for ns = 1:numsec
securityNumberString = num2str(ns);
Security{ns} = input(['Enter Security ',securityNumberString,' Ticker: '],'s');
SecurityWt(ns) = input(['Enter Security ',securityNumberString,' Weight (ex: .25): ']);
end
Notice that Security is a cell array, convenient for storing text. SecurityWt is a numeric array. Pay attention to where I used curly brackets instead of parentheses.
  5 件のコメント
the cyclist
the cyclist 2015 年 10 月 13 日
編集済み: the cyclist 2015 年 10 月 13 日
You have the label as the first argument, and the numeric value second. It should be the other way around:
pie(SecurityWt,Security)
P_Alpha
P_Alpha 2015 年 10 月 13 日
Wow I feel dumb, thanks so much.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePortfolio Optimization and Asset Allocation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by