I looked in the Matlab handbook and various posts but nothing seems to fit. You find the problematic code below.
My code runs fine, however, I am not sure about the loops for Year and PartOfYear. I want my code to run for Year = 2008:2016 and for PartOfYear = 1:2, however, when Year = 2017, it should only run for PartOfYear = 1. I.e., there is NO PartOfYear = 2 when Year = 2017. Is there a more efficient (or even correct, as mine is probably incorrect) way of doing this? At the moment I just tried to run PartOfYear = 1 again whenever it gets to Year = 2017, PartOfYear = 2, and thereby just do this part of the code twice, but I believe it is incorrect as well as inefficient, as the results seem to be off.
Thank you!
for index = 1:20
StartYearData = 2008;
EndYearData = 2017;
for Year = StartYearData:EndYearData
for PartOfYear = 1:2
if PartOfYear == 1
StartDayData = 1;
StartMonthData = 1;
EndDayData = 30;
EndMonthData = 6;
elseif PartOfYear == 2
StartDayData = 1;
StartMonthData = 7;
EndDayData = 31;
EndMonthData = 12;
% THIS IS THE WAY I USE TO TRY AND FIX MY PROBLEM
% BUT I BELIEVE IT IS WRONG OR AT LEAST INEFFICIENT:
if Year == 2017
PartOfYear = 1; StartDayData = 1; StartMonthData = 1;
EndDayData = 30; EndMonthData = 6;
end
end
% DO A LOT OF THINGS
end
if Year > 2016? It's unclear what you're asking, please show us your expected outputs for each year, or what you're actually trying to do.