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 arr

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

9 Comments

the rest of the question:
Each struct should contain three fields with these (exact) field names: “month”, “date”, and “day” (all lower case).
• The month field must contain a string with the name of the month (first letter capitalized).
• The date field must contain a scalar specifying the day of the month.
• The day field must contain the three-letter abbreviation of the day chosen from this list: 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'.
For example, here is a call of the function followed by a command that shows the seventh element of the struct array that is returned by the function:
>> m = year2016(2);
>> m(7)
ans =
month: 'February'
date: 7
day: 'Sun'
function[d] = year2016(m)
if m<1 || m>12 || ~isscalar(m)|| m~=fix(m)|| ~isvector(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','Sun'};
d = struct('day',Day(Date),'date',num2cell(1:numel(Days)));
[d(:).month] = deal(Month{m});
end
end
Problem 2 (year2016):
Feedback: Your function performed correctly for argument(s) 1
Feedback: Your function performed correctly for argument(s) 2
Feedback: Your function performed correctly for argument(s) 3
Feedback: Your function performed correctly for argument(s) 4
Feedback: Your function performed correctly for argument(s) 5
Feedback: Your function performed correctly for argument(s) 6
Feedback: Your function performed correctly for argument(s) 7
Feedback: Your function performed correctly for argument(s) 8
Feedback: Your function performed correctly for argument(s) 9
Feedback: Your function performed correctly for argument(s) 10
Feedback: Your function performed correctly for argument(s) 11
Feedback: Your function performed correctly for argument(s) 12
Feedback: Your function performed correctly for argument(s) 0
Feedback: Your function performed correctly for argument(s) -1
Feedback: Your function performed correctly for argument(s) 2.2
Feedback: Your program made an error for argument(s) [1 2]
Your solution is _not_ correct.
> m = [1 2]
m =
1 2
>> year2016(m)
Operands to the || and && operators must be convertible to logical
scalar values.
Error in year2016 (line 3)
if m<1 || m>12 || ~isscalar(m)|| m~=fix(m)
What does your function return when you call it with the vector [1 2] as input?
What does the statement of your homework assignment indicate your function should return when you call it with the vector [1 2] as input? Nothing in the text that you've posted states what the expected result should be.
If the input is not an integer between 1 and 12, the function returns the empty array.
function[d] = year2016(m)
if m<1 || m>12 || ~isscalar(m)|| m~=fix(m)
d = [];
The full question listed in the HW is posted above, underneath my code
In this case, order matters due to the short circuiting behavior of the || operator.
m = [1 2];
This section of code will error. The expression "m < 1" returns a vector not a scalar, and the || operator requires the two values with which it's computing to be scalar.
if m < 1 || ~isscalar(m)
disp('wrong!')
end
This section of code will work and will display 'wrong!'. The expression "~isscalar(m)" returns true, which is a logical scalar value. Since "true or <something else>" is true, MATLAB doesn't need to evaluate the second part of the expression. Therefore the fact that "m < 1" would have returned a non-scalar when evaluated doesn't matter. It doesn't get that far.
if ~isscalar(m) || m < 1
disp('wrong!')
end
If m were a scalar, "~isscalar(m)" would be false. Now MATLAB isn't sure whether the expression should be true or false based solely on the first expression's value, so it has to continue on to the second expression. But now we're guaranteed that "m < 1" returns a scalar which || can handle.

Sign in to comment.

 Accepted 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.

More Answers (0)

Asked:

on 4 Jan 2019

Commented:

on 4 Jan 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!