Record iterations of loop that has a constant numel

1 回表示 (過去 30 日間)
Noah
Noah 2012 年 6 月 27 日
I have a loop that cycles through time advancing 126 days each time to calculate returns of stock prices. I would like to record the returns at each interval, however, since the number of element in the loop remains constant, I am getting an index out of bounds error. Any idea to how to fix that? Here is a sample of my code (the actual one is quite a bit longer):
for z = 1:126:883
[num, txt] = xlsread('GDX_ETF_Sector_MeanRev.xlsx', 'GLD');
start = strmatch('5/31/2006', txt(1:end), 'exact')+z;
finish = start+252;
[num, txt] = xlsread('GDX_ETF_Sector_MeanRev.xlsx', 'GLD', ['A' num2str(Istart) ':B' num2str(finish)'']);
returns = price2ret(num);
end
If I change it to returns(z) I get an error: In an assignment A(I) = B, the number of elements in B and I must be the same. If I fix this error then I get index out of bounds because numel in z remains constant.
Thanks a lot for your help!

採用された回答

Tom
Tom 2012 年 6 月 27 日
It's not entirely clear to me, but it looks like the variable num will have a length of 252. I think the best way would be to store it in a large matrix, or a cell:
count=0;
for z = 1:126:883
count=count+1;
...
returns{count}=price2ret(num);
or
returns(count,:)=price2ret(num);
end
  1 件のコメント
Noah
Noah 2012 年 6 月 27 日
Works perfectly, thanks a lot!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by