MATLAB: 2. Write a function called year2016 that returns a row-vector of struct-s whose elements correspond to the days of a month in 2016 as specified by the input argument. If the input is not an integer between 1 and 12, the function returns the empty a

hw7year2016

Please explain to me why this code will not work.
function[d] = year2016(m)
if m<1 || m>12 || ~isscalar(m)|| m~=fix(m)
d = [];
else days = datenum([2016,m,1]):datenum([2016,m+1,1])-1;
date = 1 + mod(days-3,7);
month = {'January', 'february','March', 'April', 'May', 'June', 'July','August', 'September','October', 'November', 'December'};
day = {'Mon', 'Tue', 'Wed','Thu', 'Fri','Sat'};
x = day(date);
y = num2cell(1:numel(days));
z = month(m);
d = struct('day',x, 'date',y,'month',z);
end

Best Answer

If m is not a scalar, the comparison:
if m<1 || m>12 ...
fails, because the || operator can be applied to scalars only. Move the check for scalar inputs to the front:
if ~isscalar(m) || m<1 || m>12 || m~=fix(m)
Now the first condition is true for a non-scalar input already and the rest of the condition is not evaluated anymore.