0

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
5
  • If I understand correctly is the first part of year (month 1-6) always 30 days long for each month? and months 7-12 are 31 days? Is this what you want it to do? Commented Aug 24, 2018 at 11:01
  • 1
    Also regarding the loops have you tried using 'switch' (see uk.mathworks.com/help/matlab/ref/switch.html) ? That might be a slightly better way. so you can have something alike for switch Year, case 2017 Commented Aug 24, 2018 at 11:03
  • Looks like you just want to use 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. Commented Aug 24, 2018 at 11:17
  • I edited my question! (even though it was already quite clear: I wanted to "make sure that my code runs from 2008 to 2016 for PartOfYear = 1:2 and in 2017 only for PartOfYear = 1") I've never worked with switch - are you sure it's a good way? And is my way just inefficient or could the results be wrong because "loop index is changed inside a for loop"? Commented Aug 24, 2018 at 19:06
  • @Wolfie yes, that's a possibility, but how can I nest the for-loop with the if-statement? I tried different ways and couldn't do it. Commented Aug 24, 2018 at 19:19

1 Answer 1

1

You can't change a loop variable inside the loop. It looks like you're just getting confused with the ordering of the loops and the if statement.

There are lots of ways to achieve what you've asked. This code should do what you want, and only use PartOfYear = 1 (not =1:2) when Year = 2017:

for index = 1:20    
    StartYearData = 2008;
    EndYearData = 2017;    
    for Year = StartYearData:EndYearData 
        % If the year is 2017, we only want to use PartOfYear=1, otherwise 1:2
        if Year == 2017
            parts = 1;
        else
            parts = 1:2;
        end
        % Now we can loop over the 'parts', which is either 1 or 1:2
        for PartOfYear = parts 
            StartDayData = 1;   % This is the same regardless of PartOfYear value 
            if PartOfYear == 1
                StartMonthData = 1;
                EndDayData = 30;
                EndMonthData = 6;    
            elseif PartOfYear == 2
                StartMonthData = 7;
                EndDayData = 31;
                EndMonthData = 12;    
            end     
            % Stuff...
        end
    end
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.