Info

此问题已关闭。 请重新打开它进行编辑或回答。

Error for loop trough cell array

1 次查看(过去 30 天)
Andrea Tersigni
Andrea Tersigni 2017-2-24
关闭: MATLAB Answer Bot 2021-8-20
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
c=yahoo;
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');
plot(d(:,1),d(:,2),'LineWidth',2);
datetick;title('Closing price');ylabel('USD');
end
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);

回答(1 个)

Guillaume
Guillaume 2017-2-24
" 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