MATLAB: Execute a for loop over a certain time period.

datefor looplooptimewhle loop

I use a function called uigetdate to get the date as a number (e.g. 736850). I then want to find the number of elapsed days for any given year, starting with jan 1. My code is as follows and I would rather put it in a loop than have to keep typing different years (the only thing that changes are the years). note: GMT_day1 equals something like 164. How do I make a loop for this?
day1 = uigetdate;
day1 = floor(day1);
day2 = uigetdate;
day2 = floor(day2);
if day1 > datenum(2016,12,31) && day2 < datenum(2017,12,31)
time1 = datenum(2016,12,31);
GMT_day1 = day1 - time1;
GMT_day2 = day2 - time1;
elseif day1 > datenum(2017,12,31) && day2 < datenum(2018,12,31)
time1 = datenum(2017,12,31);
GMT_day1 = day1 - time1;
GMT_day2 = day2 - time1;

Best Answer

Does anyone know how to make a loop that changes the year for a hundred iterations?
I'm really not sure what you're asking. Certainly, the question above is trivially solved:
for year = 2016:2016+100
%do something with year
end
edit: however, if all you want to do is calculate the number of days between a data and the start of the year then loops and ifs are absolutely not needed. Using modern datetime instead of the outdated datenum:
d = datetime(uigetdate(), 'ConvertFrom', 'datenum');
%set time to 00:00:00 (optional if just calculating number of days elapsed since start of year)
d = dateshift(d, 'start', 'day');
%calculate numbers of days since start of same year
elapseddays = days(d - dateshift(d, 'start', 'year'));