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:
Loop works exactly one time

Subject: Loop works exactly one time

From: Ryan

Date: 22 Jun, 2014 17:40:08

Message: 1 of 5

My loop works exactly one time. Can someone take a look and let me know what's wrong here. I'm guessing that I need to reset a variable, or clear a variable, or something along these lines.

elm = sp500
for i = 1:length(elm)
curstate=elm{i}
price = hist_stock_data('01012000','31122000',curstate)
y = diff(log(price.Close(end:-1:1)))
end


One the first run through, everything is fine. The second time through I get this.
Reference to non-existent field 'Close'.

Error in Untitled (line 13)
y = diff(log(price.Close(end:-1:1)))


Thanks everyone!

Subject: Loop works exactly one time

From: dpb

Date: 22 Jun, 2014 18:37:57

Message: 2 of 5

On 6/22/2014 12:40 PM, Ryan wrote:
> My loop works exactly one time. Can someone take a look and let me know
> what's wrong here. I'm guessing that I need to reset a variable, or
> clear a variable, or something along these lines.
>
> elm = sp500
> for i = 1:length(elm)
> curstate=elm{i}
> price = hist_stock_data('01012000','31122000',curstate)
> y = diff(log(price.Close(end:-1:1)))
> end
>
>
> One the first run through, everything is fine. The second time through I
> get this.
> Reference to non-existent field 'Close'.
>
> Error in Untitled (line 13)
> y = diff(log(price.Close(end:-1:1)))

Have you verified it returns anything at all for the second case? I'd
guess the loop itself is immaterial but that you'd get the same result
if you started with i=2 instead of 1

--

Subject: Loop works exactly one time

From: Ryan

Date: 22 Jun, 2014 22:38:13

Message: 3 of 5

dpb <none@non.net> wrote in message <lo77q2$tmc$2@speranza.aioe.org>...
> On 6/22/2014 12:40 PM, Ryan wrote:
> > My loop works exactly one time. Can someone take a look and let me know
> > what's wrong here. I'm guessing that I need to reset a variable, or
> > clear a variable, or something along these lines.
> >
> > elm = sp500
> > for i = 1:length(elm)
> > curstate=elm{i}
> > price = hist_stock_data('01012000','31122000',curstate)
> > y = diff(log(price.Close(end:-1:1)))
> > end
> >
> >
> > One the first run through, everything is fine. The second time through I
> > get this.
> > Reference to non-existent field 'Close'.
> >
> > Error in Untitled (line 13)
> > y = diff(log(price.Close(end:-1:1)))
>
> Have you verified it returns anything at all for the second case? I'd
> guess the loop itself is immaterial but that you'd get the same result
> if you started with i=2 instead of 1
>
> --


Wow. You are right. I didn't think of that.

If I start with the 2nd element, it doesn't work but if I start with the 3rd element it works (the 1st element works fine). The 4th doesn't work; the 5th & 6th both work. Every time it doesn't work, I get this (below).

price =

0x0 struct array with no fields.

Reference to non-existent field 'Close'.

Error in Untitled (line 13)
y = diff(log(price.Close(end:-1:1)))

What the heck is going on here???

Subject: Loop works exactly one time

From: dpb

Date: 22 Jun, 2014 23:19:41

Message: 4 of 5

On 6/22/2014 5:38 PM, Ryan wrote:
> dpb <none@non.net> wrote in message <lo77q2$tmc$2@speranza.aioe.org>...
...

>> Have you verified it returns anything at all for the second case? I'd
>> guess the loop itself is immaterial but that you'd get the same result
>> if you started with i=2 instead of 1

> Wow. You are right. I didn't think of that.
>
> If I start with the 2nd element, it doesn't work but if I start with the
> 3rd element it works (the 1st element works fine). The 4th doesn't work;
> the 5th & 6th both work. Every time it doesn't work, I get this (below).
>
> price =
> 0x0 struct array with no fields.
>
> Reference to non-existent field 'Close'.

Well, if there is no struct, clearly it can't have a field 'Close'...

...

> What the heck is going on here???

Your retrieval process isn't working for certain values of the array
you're using to identify desired returns. Why, specifically, I don't
have any way of knowing but in

elm = sp500
for i = 1:length(elm)
   curstate=elm{i}
   price = hist_stock_data('01012000','31122000',curstate);

What is sp500 and how's it defined?

Is it a cell array of strings (would seem to almost have to be, but you
don't show it so have to guess)?

Use the debugger and step thru and see if it's returning the expected
value from the array or whether there are missing values in it or the
function expects an exact match and there's a character off or a blank
or something causing a failure, or somesuch.

You'll just have to work thru that process to see why it fails for some
reason.

You could, of course, fix up the loop to retrieve what does work and go
on just by checking the result--

for i=1:length(elm)
   price = hist_stock_data('01012000','31122000',elm(i));
   if isempty(price), continue, end
   ...

--

Subject: Loop works exactly one time

From: Ryan

Date: 22 Jun, 2014 23:51:06

Message: 5 of 5

dpb <none@non.net> wrote in message <lo7oae$5jf$1@speranza.aioe.org>...
> On 6/22/2014 5:38 PM, Ryan wrote:
> > dpb <none@non.net> wrote in message <lo77q2$tmc$2@speranza.aioe.org>...
> ...
>
> >> Have you verified it returns anything at all for the second case? I'd
> >> guess the loop itself is immaterial but that you'd get the same result
> >> if you started with i=2 instead of 1
>
> > Wow. You are right. I didn't think of that.
> >
> > If I start with the 2nd element, it doesn't work but if I start with the
> > 3rd element it works (the 1st element works fine). The 4th doesn't work;
> > the 5th & 6th both work. Every time it doesn't work, I get this (below).
> >
> > price =
> > 0x0 struct array with no fields.
> >
> > Reference to non-existent field 'Close'.
>
> Well, if there is no struct, clearly it can't have a field 'Close'...
>
> ...
>
> > What the heck is going on here???
>
> Your retrieval process isn't working for certain values of the array
> you're using to identify desired returns. Why, specifically, I don't
> have any way of knowing but in
>
> elm = sp500
> for i = 1:length(elm)
> curstate=elm{i}
> price = hist_stock_data('01012000','31122000',curstate);
>
> What is sp500 and how's it defined?
>
> Is it a cell array of strings (would seem to almost have to be, but you
> don't show it so have to guess)?
>
> Use the debugger and step thru and see if it's returning the expected
> value from the array or whether there are missing values in it or the
> function expects an exact match and there's a character off or a blank
> or something causing a failure, or somesuch.
>
> You'll just have to work thru that process to see why it fails for some
> reason.
>
> You could, of course, fix up the loop to retrieve what does work and go
> on just by checking the result--
>
> for i=1:length(elm)
> price = hist_stock_data('01012000','31122000',elm(i));
> if isempty(price), continue, end
> ...
>
> --



Yes, you are exactly right. I have all the constituents of the S&P500 in a Vector. I'm importing historical stock prices from January 1, 2000 until December 31, 2009. As it turns out, the 2nd element is ABBV and this page shows the closing prices from January 1, 2000 until December 31, 2009. The stock didn't exist back then.

http://finance.yahoo.com/q/hp?s=ABBV&a=11&b=31&c=2009&d=05&e=22&f=2000&g=d

It went public in early 2013 and now it's a component of the S&P500. Well, at least I know what's going on now.

Thanks so much!!

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