Date in a loop

Hi All,
I am using the following code to use a sorted file which matches elements of sorted file with financial time series table/objects of two price series. I don't know how to run a loop which changes dates as well alongwith matrix elements. The code is as follows:
for r= 2:40
for c=1:5
if sorted{c,r}=='Alpha';
sortednew{c,r}= fts2mat(Apple.ARG('1-Jan-0019')) -fts2mat(Applenew.ARG('1-Jan-0019'));
elseif sorted{c,r}=='Beta';
sortednew{c,r}=fts2mat(Apple.BXD('1-Jan-0019')) - fts2mat(Applenew.BXD('1-Jan-0019'));
elseif sorted{c,r}=='theta';
sortednew{c,r}=fts2mat(Apple.ASD('1-Jan-0019')) - fts2mat(Applenew.ASD('1-Jan-0019'));
else sorted{c,r}=='gamma';
sortednew{c,r}=fts2mat(Apple.STA('1-Jan-0019')) - fts2mat(Applenew.STA('1-Jan-0019'));
end;
end;
end;
I want to add a date loop after the very first loop which increments the date by 7days (weekly) and that incremented date is then used for indexing in the forthcoming statements in place of '1-Jan-0019':
sortednew{c,r}= fts2mat(Apple.ARG('1-Jan-0019')) -
fts2mat(Applenew.ARG('1-Jan-0019'))
Any help will be highly appreciated
Regards,
Amd.

1 件のコメント

Jan
Jan 2012 年 4 月 14 日
Do not use "sorted{c,r}=='Alpha'" to compare strings, because this fails if the number of characters do not match. Use STRCMP instead.

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

 採用された回答

Walter Roberson
Walter Roberson 2012 年 4 月 14 日

0 投票

startdatevec = [19, 1, 1]; %year 19 ??, month 1, day 1
for weekno = 1 : 20
startedate = datestr(startedatevec);
[....]
startedatevec(3) = startdatevec(3) + 7;
end
Don't worry about the date number becoming 36 and 43 and so on: datestr will automatically do the proper wrapping to find the right day of the right month.

3 件のコメント

ARS
ARS 2012 年 4 月 15 日
Hi Walter,
Thanks for your answer. Can you please provide help on this? I am trying to get dates from a fints table. The dates move through the very first loop. But soon after running the program I get the error:
Error using fints/subsref (line 142)
Invalid dates being referenced. It could be that
the input is not in a column vector or cell array.
for r= 2:40
datee= datestr(mytable.dates(r))
for c=1:5
if sorted{c,r}=='Alpha';
sortednew{c,r}= fts2mat(Apple.ARG('Datee')) -fts2mat(Applenew.ARG('Datee'));
elseif sorted{c,r}=='Beta';
sortednew{c,r}=fts2mat(Apple.BXD('Datee')) - fts2mat(Applenew.BXD('Datee'));
elseif sorted{c,r}=='theta';
sortednew{c,r}=fts2mat(Apple.ASD('Datee')) - fts2mat(Applenew.ASD('Datee'));
else sorted{c,r}=='gamma';
sortednew{c,r}=fts2mat(Apple.STA('Datee')) - fts2mat(Applenew.STA('Datee'));
end;
end;
end;
the values in 'datee' are not being picked up for fints referencing.
Need help on this issue.
Regards,
Amd.
Walter Roberson
Walter Roberson 2012 年 4 月 15 日
Do not quote Datee.
sortednew{c,r}= fts2mat(Apple.ARG(dateee)) -fts2mat(Applenew.ARG(datee));
ARS
ARS 2012 年 4 月 15 日
Yes this helps....Thanks for the answer....
Also I found another good way to do that:
sortednew{c,r}= fts2mat(Apple.ARG(Spot.dates(r)) -fts2mat(Applenew.ARG(Spot.dates(r)));
No need to introduce datee.....and 'r' loops 40 times(takes values from the very first loop), date loops 40 times.
Thanks again Walter.

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

その他の回答 (1 件)

ARS
ARS 2012 年 4 月 15 日

0 投票

Also I found another good way to do that:
sortednew{c,r}= fts2mat(Apple.ARG(Spot.dates(r)) - fts2mat(Applenew.ARG(Spot.dates(r)));
No need to introduce datee.....and 'r' loops 40 times(takes values from the very first loop), date loops 40 times.

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by