```Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: The variable 'ClosePrice' appears to change in size on every loop iteration.
Date: Mon, 5 Nov 2012 19:37:07 +0000 (UTC)
Organization: HSBC
Lines: 124
Message-ID: <k794h3\$es0\$1@newscl01ah.mathworks.com>
NNTP-Posting-Host: www-01-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1352144227 15232 172.30.248.46 (5 Nov 2012 19:37:07 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 5 Nov 2012 19:37:07 +0000 (UTC)
Xref: news.mathworks.com comp.soft-sys.matlab:782094

Hello Matlab experts!  I’m following the video tutorial here:
http://www.mathworks.com/videos/getting-started-with-portfolio-optimization-68762.html

I retyped all the code that is visible on the screen (I couldn’t find any place to download it).

Here is all of the code:
c = yahoo

ticker = {'MMM', 'AA', 'AXP', 'T', 'BAC', 'BA', 'CAT', 'CVX', 'CSCO', 'KO', 'DD' 'XOM', 'GE', 'HPQ', 'HD'}
%%'INTC', 'IBM', 'JPM', 'JNJ', 'KFT', 'MCD', 'MRK' ...
%%'MSFT', 'PFE', 'PG', 'TRV', 'UTX', 'VZ', 'WMT', 'DIS'}

%for i = 1:30
for i = 1 : 15
temp = Price.(ticker{i});
ClosePrice(:,i) = temp(:,2);
end

Returns = price2ret(ClosePrice);

plot(Returns(:,1:2));
ylabel('Monthly Return');
xlabel('Month');
legend('3M', 'Alcoa');
title('3M vs Alcoa Monthly Returns')

p = (Portfolio('Name', 'DJIA 30'));
p = p.setAssetList(ticker);
disp(p);

p = p.estimateAssetMoments(Returns);
p = p.setDefaultConstraints;

[prisk, preturns] = p.plotFrontier(10);
[pwvt] = p.estimateFrontier(10);
hold on
plot(prisk, preturns, 'ored');

A = zeros(size(ticker));
A([1 10 15]) = 1;
b = .1;

p = p.setEquality(A, b);

p = Portfolio(p,'AEquality', A, 'bEquality', b);

hold on
p.plotFrontier;

C = zeros(size(ticker));
C([10, 15]) = 1;
d = .05;

p = p.setInequality(C,d);

p = Portfolio(p, 'AInequality', C, 'bInequality', d);

hold on
p.plotFrontier;

G = zeros(size(ticker));
G([10, 12, 15]) = 1;

p = p.setGroups(G, 0.1, 0.5);
p = Portfolio(p, 'GroupMarix', G, 'LowerGroup', 0.1, 'UpperGroup', 0.5);

hold on
p.plotFrontier;

lb = -.05;
ub = 0.10;

%%Method #1
p = p.setBounds(lb,ub);

%%Method #2
p = Portfolio(p, 'LowerBound', lb, 'UpperBound', ub);

hold on
p.plotFrontier;
%%format_plots();

%%Method #1
p = p.setBudget(0.95, 1.05);

hold on
p.plotFrontier;

bc = 0.005;
sc = 0.007;
X_o = 0.03*ones(1,15);

p = p.setInitPort(X_o);
p = p.setCosts(bc,sc);

[pwgt, pbuy, psell] = p.estimateFrontier (4);

q = p;
p = Portfolio(p, 'BuyCost', 0, 'SellCost', 0);

Those with a keen eye will notice that I’m working with 15 components of the Dow; not 30.

I kept getting an error on the following line:
ClosePrice(:,i) = temp(:,2);

This is the error:
The variable 'ClosePrice' appears to change in size on every loop iteration (within a script).

It would get up to about 17 ticker symbols, and then it errored out.  When I truncated it to 15 ticker symbols, it worked fine.  What am I missing here.  Surely, a user can analyze a portfolio of more than 15 stocks, or 17 stocks, or whatever.

Any thoughts on how to resolve the error?

Thanks to all!!
```