Changing Steps in a loop

1 view (last 30 days)
Davin
Davin on 16 Feb 2015
Answered: Davin on 17 Feb 2015
Hello Guys,
I have a small issue with a loop, which needs to have some kind of dynamic steps.
The context is the following, I am working on a 5 business days, which is normal week. Normally the calculations start on monday which is referenced as 2 in Matlab, but there are weeks where Mondays are bank holidays, so here I have my issues, because a constant step will not help if I do the following :
Heres are the data :
Mon 1416.18 733414
Tue 1390.19 733415
Wed 1409.13 733416
Thu 1420.33 733417
Fri 1401.02 733418
Mon 1416.25 733421
Tue 1380.95 733422
Wed 1373.2 733423
Thu 1333.25 733424
Fri 1325.19 733425
Tue 1310.5 733429
Wed 1338.6 733430
Thu 1352.07 733431
Fri 1330.61 733432
Mon 1353.96 733435
Tue 1362.3 733436
Wed 1355.81 733437
Thu 1378.55 733438
Fri 1395.42 733439
Mon 1380.82 733442
Code
step = 5
for i : 2 : step : n
if DayNumber == 2
calculations when first day is monday
end
if DayNumber == 3 ( tuesday)
do operation
step = step -1
end
end
so here normally, i expected an i of 2,7,12,16( Not 17), then 21( 16+5, back to step 5)
Unfortunately I am getting always a constant step of 5, do you know how to make the loop reactive to the steps.
thanks
Davin

Answers (2)

Image Analyst
Image Analyst on 16 Feb 2015
The code doesn't run. The data needs to be a cell array and you didn't do that for us, so I'm not even going to try it. Basically if your loop step/increment/counter needs to vary during each iteration then you can't use a "for" loop - you need to use a "while" loop. Make your variable defined correctly and I might fix it for you.

Davin
Davin on 17 Feb 2015
Hello,
Thanks for your answer. As the code is long I didnt want to paste it all, yes a while loop could help, but I am having difficulties to implement it.
You will see attached to this message, the mat environment plus the testpattern.m.
to launch it just type pattern(GSPC), the issue comes around row 17, where it should iterate to 21, but it does start at 22, which is wrong.
The code is a bit bulky because it defines a number of possible paths..
thanks very much.
Davin

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!