This question is closed. Reopen it to edit or answer.

Error for loop trough cell array

1 view (last 30 days)
Andrea Tersigni
Andrea Tersigni on 24 Feb 2017
Closed: MATLAB Answer Bot on 20 Aug 2021
Hello I have this 1x10 cell array named ticker_list: ('GE' 'BAC' 'VALE' 'AMD' 'PFE' 'F' 'INTC' 'NOK' 'BABA' 'UL') and I have to make the for loop through every cell take the data from yahoo finance and plot. When I run the code below i receive an error and the plotting stops at the sixth security. The error is:
if true
for sec = 'ticker_list'
field = 'adj Close';
date_begin = datenum(2012,12,31);
date_end = datenum(2016,12,31);
d = fetch(c,sec,field,date_begin,date_end);
figure('Name','Closing Price');
datetick;title('Closing price');ylabel('USD');
Error using yahoo/fetch (line 387)
Unable to return historical data for given security.
Error in untitled (line 7)
d = fetch(c,sec,field,date_begin,date_end);

Answers (1)

Guillaume on 24 Feb 2017
" the plotting stops at the sixth security"
for sec = 'ticker_list'
iterates over the characters of the char array 'ticker_list' (so sec is at first 't', then 'i', etc.). The sixth character is '_'. I assume that 't', 'i', etc. can be matched to a ticker, but not '_'. Of course, none of this is what you want. I suspect your code should be
for sec = ticker_list %iterate over the columns of the cell array instead of characters of a string
d = fetch(c, sec{1}, field, date_begin, date_end);
Note: Learn to use the debugger. Had you used it and stepped through your code as it executed, you would have noticed the problem immediately.

Community Treasure Hunt

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

Start Hunting!

Translated by