Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
The variable 'ClosePrice' appears to change in size on every loop iteration.

Subject: The variable 'ClosePrice' appears to change in size on every loop iteration.

From: Ryan

Date: 5 Nov, 2012 19:37:07

Message: 1 of 9

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
Price.(ticker{i}) = fetch(c,ticker(i),'Adj Close','01/01/2000','12/31/2010','m');
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!!

Subject: The variable 'ClosePrice' appears to change in size on every

From: dpb

Date: 5 Nov, 2012 21:04:51

Message: 2 of 9

On 11/5/2012 1:37 PM, Ryan wrote:
> 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
> Price.(ticker{i}) = fetch(c,ticker(i),'Adj
> Close','01/01/2000','12/31/2010','m');
> temp = Price.(ticker{i});
> ClosePrice(:,i) = temp(:,2);
> end
...

> 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).
...

> Any thoughts on how to resolve the error?
...

Use debugger and step through the loop and observe what's going on each
pass. That will probably lead to conclusion as to what is
intended/correction should be.

--

Subject: The variable 'ClosePrice' appears to change in size on every

From: Ryan

Date: 6 Nov, 2012 05:26:07

Message: 3 of 9


Well, that's exactly what I did. How do you think I know I errors out on the 17th time through the loop?

Can someone here copy the code and paste it into a Matlab compiler and run it. You just have to change a couple basic things:

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


What do you get? I don't understand that error message:
The variable 'ClosePrice' appears to change in size on every loop iteration.



dpb <none@non.net> wrote in message <k799j6$v17$1@speranza.aioe.org>...
> On 11/5/2012 1:37 PM, Ryan wrote:
> > 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
> > Price.(ticker{i}) = fetch(c,ticker(i),'Adj
> > Close','01/01/2000','12/31/2010','m');
> > temp = Price.(ticker{i});
> > ClosePrice(:,i) = temp(:,2);
> > end
> ...
>
> > 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).
> ...
>
> > Any thoughts on how to resolve the error?
> ...
>
> Use debugger and step through the loop and observe what's going on each
> pass. That will probably lead to conclusion as to what is
> intended/correction should be.
>
> --

Subject: The variable 'ClosePrice' appears to change in size on every

From: dpb

Date: 6 Nov, 2012 14:41:55

Message: 4 of 9

On 11/5/2012 11:26 PM, Ryan wrote:

...[top posting repaired. PLEASE don't do that--hard follow
conversation makes]...

> dpb <none@non.net> wrote in message <k799j6$v17$1@speranza.aioe.org>...
>> On 11/5/2012 1:37 PM, Ryan wrote:
>> > 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
>> > Price.(ticker{i}) = fetch(c,ticker(i),'Adj
>> > Close','01/01/2000','12/31/2010','m');
>> > temp = Price.(ticker{i});
>> > ClosePrice(:,i) = temp(:,2);
>> > end
>> ...
>>
>> > 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).
>> ...
>>
>> > Any thoughts on how to resolve the error?
>> ...
>>
>> Use debugger and step through the loop and observe what's going on
>> each pass. That will probably lead to conclusion as to what is
>> intended/correction should be.

 >
> Well, that's exactly what I did. How do you think I know I errors out on
> the 17th time through the loop?
>
> Can someone here copy the code and paste it into a Matlab compiler and
> run it. You just have to change a couple basic things:
...
> What do you get? I don't understand that error message:
> The variable 'ClosePrice' appears to change in size on every loop
> iteration.

Don't have the necessary toolboxen, sorry...

The point is when you're in debug session, what is returned by

temp = Price.(ticker{i});

on each pass and what is expected by the documentation for the later
call to Price2Ret() where the ClosePrice variable is later used?

Either there's a mismatch in sizes between calls when trying to load the
variable or there's intended to be a concatenation/growing of the array
or somesuch.

It should be relatively easy to determine what you need in the variable
based on what the later function call expects to work on and your
mission, should you choose to accept, is to see why the returned data
aren't being built in that form. Maybe there's a NULL return for one of
them or somesuch thing.

--

Subject: The variable 'ClosePrice' appears to change in size on every

From: dpb

Date: 7 Nov, 2012 01:21:26

Message: 5 of 9

On 11/5/2012 11:26 PM, Ryan wrote:
>> > for i = 1:length(ticker) % 15
>> > Price.(ticker{i}) = fetch(c,ticker(i),'Adj Close','01/01/2000','12/31/2010','m');
>> > temp = Price.(ticker{i});
>> > ClosePrice(:,i) = temp(:,2);
>> > end
      Returns = price2ret(ClosePrice);
>> ...
>>
>> > I kept getting an error on the following line:
...
>> > The variable 'ClosePrice' appears to change in size on every loop
>> > iteration (within a script).
...

OK, I had time to actually read the code more thoroughly...it shouldn't
fail I wouldn't think altho I don't have a recent-enough release to know
what actual behavior is; I'd think it would only be a warning that
haven't preallocated ClosePrice array...

The problem I see is twofold -- ClosePrice is adding a column every
iteration and the array hasn't been preallocated so indeed it is being
"growed" dynamically. Since it is an array and not a cell array, that
means that every element must have the same number of rows in the
temp(:,2) vector. This _should_ hold unless some stock were to have had
trading closed or somesuch. The problem is unless you compute the
length, you don't know precisely how many values are going to be
returned...there are a couple of ways to do this; if there's a function
in the toolbox to return the number of trading days accurately between
the two dates you could obviously use that. Failing that, you could
retrieve the first symbol, query size() on the return value and then
allocate ClosePrice and store; then doing the loop for the remainder...


for i = 1:length(ticker)
   Price.(ticker{i}) = fetch(c,ticker(i), ....
   temp = Price.(ticker{i});
   if i==1, ClosePrice=zeros(length(temp(:,2)),length(ticker));end
   ClosePrice(:,i) = temp(:,2);
end

or, moving the first outside the loop and preallocating...

Price.(ticker{1}) = fetch(c,ticker(i), .... ;
temp = Price.(ticker{1});
ClosePrice=zeros(length(temp(:,2)),length(ticker));
ClosePrice(:,1) = temp(:,2);
for 2 = 1:length(ticker)
   Price.(ticker{i}) = fetch(c,ticker(i), ....
   temp = Price.(ticker{i});
   if i==1, ClosePrice=zeros(length(temp(:,2)),length(ticker));end
   ClosePrice(:,i) = temp(:,2);
end

I suspect the warning on growing the array must have been turned off or
the warning ignored during the session in the video.

--

Subject: The variable 'ClosePrice' appears to change in size on every

From: Ryan

Date: 7 Nov, 2012 15:02:08

Message: 6 of 9

You are correct, the array is growing dynamically, and yes, every element must have the same number of rows in the temp(:,2) vector. I agree about the length of the array and I tried both of your proposed solutions. I got the same error. So, I looked at this even closer. It seems ‘JNJ’ is the offender. I took that out, and changed the # of times the loop runs: for i = 1:30

Now, everything works fine.

Something with ticker JNJ is different from the other stock tickers.

Thanks for the help with this, dpb.



dpb <none@non.net> wrote in message <k7ccvs$bhk$1@speranza.aioe.org>...
> On 11/5/2012 11:26 PM, Ryan wrote:
> >> > for i = 1:length(ticker) % 15
> >> > Price.(ticker{i}) = fetch(c,ticker(i),'Adj Close','01/01/2000','12/31/2010','m');
> >> > temp = Price.(ticker{i});
> >> > ClosePrice(:,i) = temp(:,2);
> >> > end
> Returns = price2ret(ClosePrice);
> >> ...
> >>
> >> > I kept getting an error on the following line:
> ...
> >> > The variable 'ClosePrice' appears to change in size on every loop
> >> > iteration (within a script).
> ...
>
> OK, I had time to actually read the code more thoroughly...it shouldn't
> fail I wouldn't think altho I don't have a recent-enough release to know
> what actual behavior is; I'd think it would only be a warning that
> haven't preallocated ClosePrice array...
>
> The problem I see is twofold -- ClosePrice is adding a column every
> iteration and the array hasn't been preallocated so indeed it is being
> "growed" dynamically. Since it is an array and not a cell array, that
> means that every element must have the same number of rows in the
> temp(:,2) vector. This _should_ hold unless some stock were to have had
> trading closed or somesuch. The problem is unless you compute the
> length, you don't know precisely how many values are going to be
> returned...there are a couple of ways to do this; if there's a function
> in the toolbox to return the number of trading days accurately between
> the two dates you could obviously use that. Failing that, you could
> retrieve the first symbol, query size() on the return value and then
> allocate ClosePrice and store; then doing the loop for the remainder...
>
>
> for i = 1:length(ticker)
> Price.(ticker{i}) = fetch(c,ticker(i), ....
> temp = Price.(ticker{i});
> if i==1, ClosePrice=zeros(length(temp(:,2)),length(ticker));end
> ClosePrice(:,i) = temp(:,2);
> end
>
> or, moving the first outside the loop and preallocating...
>
> Price.(ticker{1}) = fetch(c,ticker(i), .... ;
> temp = Price.(ticker{1});
> ClosePrice=zeros(length(temp(:,2)),length(ticker));
> ClosePrice(:,1) = temp(:,2);
> for 2 = 1:length(ticker)
> Price.(ticker{i}) = fetch(c,ticker(i), ....
> temp = Price.(ticker{i});
> if i==1, ClosePrice=zeros(length(temp(:,2)),length(ticker));end
> ClosePrice(:,i) = temp(:,2);
> end
>
> I suspect the warning on growing the array must have been turned off or
> the warning ignored during the session in the video.
>
> --

Subject: The variable 'ClosePrice' appears to change in size on every

From: dpb

Date: 7 Nov, 2012 15:14:57

Message: 7 of 9

On 11/7/2012 9:02 AM, Ryan wrote:

...[ PLEASE Do NOT toppost... :( ]...

...

> solutions. I got the same error. So, I looked at this even closer. It
> seems ‘JNJ’ is the offender. I took that out, and changed the # of times
> the loop runs: for i = 1:30

...

Which, of course, if you had done as suggested via debug earlier you
would have discovered as well as if you looked at the results obtained
you would know just what, specifically, _is_ the difference.

For the loop index use

i=1:length(array_name)

then don't have to count manually nor change code when list changes...

--

Subject: The variable 'ClosePrice' appears to change in size on every

From: dpb

Date: 7 Nov, 2012 15:21:16

Message: 8 of 9

On 11/7/2012 9:02 AM, Ryan wrote:

...[ PLEASE Do NOT toppost... :( ]...

...

> solutions. I got the same error. So, I looked at this even closer. It
> seems ‘JNJ’ is the offender. I took that out, and changed the # of times
> the loop runs: for i = 1:30

...

Which, of course, if you had done as suggested via debug earlier you
would have discovered as well as if you looked at the results obtained
you would know just what, specifically, _is_ the difference.

For the loop index use

i=1:length(array_name)

then don't have to count manually nor change code when list changes...

--

Subject: The variable 'ClosePrice' appears to change in size on every

From: Ryan

Date: 8 Nov, 2012 03:41:10

Message: 9 of 9

Whoa!! That's hellza cool!! I didn't know you can do that. I came her form a VBA background; I know VB.NET and C#.NET reasonably well. I don;t know Matlab as well. I guess it's time to start learning this stuff in earnest.

Thanks for everything!!!

dpb <none@non.net> wrote in message <k7du6a$vg7$1@speranza.aioe.org>...
> On 11/7/2012 9:02 AM, Ryan wrote:
>
> ...[ PLEASE Do NOT toppost... :( ]...
>
> ...
>
> > solutions. I got the same error. So, I looked at this even closer. It
> > seems ‘JNJ’ is the offender. I took that out, and changed the # of times
> > the loop runs: for i = 1:30
>
> ...
>
> Which, of course, if you had done as suggested via debug earlier you
> would have discovered as well as if you looked at the results obtained
> you would know just what, specifically, _is_ the difference.
>
> For the loop index use
>
> i=1:length(array_name)
>
> then don't have to count manually nor change code when list changes...
>
> --

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us