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

This is my code. Could you please tell me
function m = year2016
for i = 1:31
[MonthNumber, DateName] = weekday(datenum([2016 1:12 i]));
m.(i) = struct('month','MonthNumber','date',i,'day', DateName);
end

 Accepted Answer

This code does the job (copied from my answer to this question, so this code is already in the public domain)
function out = year2016(m)
VN = datenum([2016,m,1]):datenum([2016,m+1,1])-1;
DN = 1+mod(VN-3,7);
MC = {'January';'February';'March';'April';'May';'June';'July';'August';'September';'October';'November';'December'};
DC = {'Mon','Tue','Wed','Thu','Fri','Sat','Sun'};
out = struct('day',DC(DN),'date',num2cell(1:numel(VN)));
[out(:).month] = deal(MC{m});
end
And tested:
>> m = year2016(12); m(8)
ans =
day = Thu
date = 8
month = December
>> m = year2016(1); m(1)
ans =
day = Fri
date = 1
month = January
>> m = year2016(2); m(29)
ans =
day = Mon
date = 29
month = February
>> m = year2016(4); m(1)
ans =
day = Fri
date = 1
month = April

17 Comments

@Wasi von Deutschland: I just did 99% of this homework task for you, and you still want me to do the remaining 1% as well? I am sure that you can fix that yourself. Hint: check the input value using an if statement:
if m>=?? && m<=??
...
else
...
end
... So learn enough MATLAB to add in error checking. It is your homework assignment. We are not a homework-writing service here.
2.2? it's in between 0-12 but how can I filter only whole numbers?
to check for whole numbers:
fix(N)==N
or simply round them to an integer: pick what suits your needs best.
I've already used this method but it doesn't work. I think question states that if argument is 2.2 etc it should also be [] as we have done with 0. I other words question should accept only whole number rest should to else branch and answer [].
So check if the input is a whole number, and add this to the if statement:
if ?? && ?? && fix(m)==m
...
else
...
end
Note that you already wrote this in words: "should accept only whole number rest should to else branch and answer []." If you can write it in words, then you can put it into code.
it went through that and stuck for argument [1 2]. Do you think it should also go to the else branch?
@Wasi von Deutschland: why are you asking me what I think? You should do exactly as your assignment tells you: it says "if the input is not an [i.e. a scalar] integer [not rational] between 1 and 12". So far we have discussed how to check for the 1 and 12, and the integer. Now all that remains is to check that the input is scalar...
(hint: numel, isscalar, etc)
isscalar gives value of isscalar(2.2)=1 and numel(2.2)=1 But how can I write a function ''when it is fraction it should go to else part''. which function can help me in this case?
@Wasi von Deutschland: that is the exact opposite of what you should do: it is simpler to test that an input matches what it should be, rather than building an infinite list of things it should not be. In any case, I already answered your question in my comment above. Here it is again, just in case you missed it:
if isscalar(??) && ?? && ?? && fix(m)==m
...
else
...
end
(I added the isscalar too, leaving nothing for you to do but "join-the-dots")
champion2015's "Answer" moved here:
I don't exactly understand this question, or the answer provided by Stephen. What is the purpose of VN and DN? Why is the month not included inside the struct? I've had a look at the struct examples in matlab help and I think I understand them more or less... Finally, why is it necessary to leave the out(:) as a column, doesn't this occur automatically? Also, how would you do it for the 'month' to appear first in the command window?
" What is the purpose of VN and DN?"
VN is a vector of the serial date number for every day of the requested month.
DN is a vector indicating the day of the week for every day of the requested month (Monday=1).
"Why is the month not included inside the struct?"
The last code line allocates the month string to every element of the structure:
[out(:).month] = deal(MC{m});
This simply allocates one string to all structure elements without needing to figure out what size the structure array is. It saves having to use repmat or the like.
"why is it necessary to leave the out(:) as a column"
out is not a column, because the colon notation is being used for allocation of values into the structure (i.e. on the LHS), not for indexing to get elements out of the structure (i.e. on the RHS). Therefore the structure's shape does not change.
"doesn't this occur automatically?"
No. It doesn't happen anywhere in my code. Lets have a look at the size:
>> S = year2016(3);
>> size(S)
ans =
1 31
Nope, definitely not a column. The question clearly states that the function "...returns a row-vector of struct-s...", which is what my code does. Why do you think that the output is a column array?
"how would you do it for the 'month' to appear first in the command window?"
Either define it first in the code (needs repmat, or a vector of indices) or use orderfields.
sorry, after reading the comments here I still dont understand the following things:
1 - DN - why to know the day we need the argument 1+mod(VN-3,7) ?
2 - why do we need to get 'date' as a cell (why num2cell)?
3 - why do we have to use the deal function for the month?
thank you and sorry for repeating previous questions. would be great if someone could explain these points in more detail.
@Levito:
  1. That mod term converts the vector of serial date numbers into a vector of weekdays. Compare the values of VN and DN yourself by printing them in the command window: VN are serial data numbers (i.e. days since the imaginary day 0th Jan year 0) whereas DN are the weekdays (Monday==1).
  2. struct creates a structure with the same size as its input cell arrays. So to get a 1xN structure (which is what the assignment asked for) I converted the 1xN date numeric vector into a 1xN cell array using num2cell, which struct will convert into a 1xN structure.
  3. deal allocates one variable on the RHS (the month name) to multiple variables on the LHS (the month field of each element of the structure). In this case the LHS is defined using a comma-separated list:

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!