MATLAB: Find out if number is divisible by x

divisors

I'm trying to write a function that determines if the input is a valid date, and for that I need a function that will tell me if an input number is divisible by 4 and at the same time not divisible by 100 (this has to do with leap years but I'm not supposed to use the special function for that). This is what I came up with but I can already see that it's not correct. In non-code language, I would like it to be something like "If divisors(year) contains the number 4 and does not contain the number 100, display the following…" but I did not know how to do this.
(This is just the part of code I'm struggling with, I didn't paste everything because it was quite a lot.)
Any tips on how I should fix it would be much appreciated!
function valid = valid_date(year,month,day)
if divisors(year) = 4 ~ 100
.
.
.
.
.
end

Best Answer

Thank you for your help so far! So now I came up with this and it works with some dates but not with most; For example, I tried it with the input (2001,1,32) and it returns "1" although the input is not a correct date.
Also sometimes I'm getting the error message Output argument "valid" (and maybe others) not assigned during call to "valid_date".
I don't know what that message means and what else is wrong in my code, maybe someone can point out where the mistake is?
function valid = valid_date(year,month,day)
% leap year: divisible by 4, not by 100 -> February has 29 days
if mod(year,4) == 0 & mod(year,100) == ~0;
if month == 2;
if 1 <= day <= 29;
valid = true;
else
valid = false;
end
elseif month == 1 | 3 | 5 | 7 | 8 | 10 | 12;
if 1 <= day <= 31;
valid = true;
else
valid = false;
end
elseif month == 4 | 6 | 9 | 11;
if 1 <= day <= 30;
valid = true;
else
valid = false;
end
else
valid = false;
end
% leap year: divisible by 400 -> February has 29 days
elseif mod(year,400) == 0;
if month == 2;
if 1 <= day <= 29;
valid = true;
else
valid = false;
end
elseif month == 1 | 3 | 5 | 7 | 8 | 10 | 12;
if 1 <= day <= 31;
valid = true;
else
valid = false;
end
elseif month == 2 | 4 | 6 | 9 | 11;
if 1 <= day <= 30;
valid = true;
else
valid = false;
end
else
valid = false;
end
% normal year -> February has 28 days
else
if month == 2;
if 1 <= day <= 28;
valid = true;
else
valid = false;
end
elseif month == 1 | 3 | 5 | 7 | 8 | 10 | 12;
if 1 <= day <= 31;
valid = true;
else
valid = false;
end
elseif month == 4 | 6 | 9 | 11;
if 1 <= day <= 30;
valid = true;
else
valid = false;
end
else
valid = false;
end
end
end
Related Question