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. 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'
My code is here;
function x = year2016(m)
year2016.month = {'January';'February';'March';'April';'May';'June';'July';'August';'September';'October';'November';'December'};
TypeList = {'Mon','Tue','Wed','Thu','Fri','Sat','Sun'};
for k = 1:numel(TypeList)
x = TypeList{k};
if month >0 && month<=12
return;
end
end
x = 'NONE';
I know that it has many error but I haven't associate month , day and date. Please help me?

3 件のコメント

Rik
Rik 2017 年 11 月 7 日
One of the many problems with this function is that your input is m, but the rest of the function uses month. You should think about the flow of your program: with an example input of 2, what are the steps your function should take before it gets to the end or a return (be aware that a return just returns control, it doesn't check if outputs are already created).
Another thing that really jumps out, is that you use a variable with the same name as the function. I believe this will return an error in future releases, but currently it is just bad programming practise.
Walter Roberson
Walter Roberson 2018 年 4 月 27 日
Please do not close questions that have an answer.

サインインしてコメントする。

回答 (2 件)

shah109
shah109 2017 年 12 月 24 日
編集済み: Walter Roberson 2017 年 12 月 25 日

1 投票

% I modified this function from stephen cobeldick post
function out = year2016(m)
if ~isscalar(m) || m <1 || m~=fix(m)
out = [];
else
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

2 件のコメント

Emma Sellers
Emma Sellers 2019 年 1 月 4 日
I have tried using a code similar to this and I get an error when the code runs
m = [1 2 ]
what needs to be added?
I tried adding sizeof(m)>1 to line three and that did not help.
Walter Roberson
Walter Roberson 2019 年 1 月 4 日
that should be caught by the isscalar test.

サインインしてコメントする。

Ugur Ulas
Ugur Ulas 2018 年 1 月 10 日
編集済み: Ugur Ulas 2018 年 1 月 10 日

0 投票

Hello, it s another solution:
function m = year2016(A,B)
day_no = datenum(2016,A,1)-datenum(2016,0,1);
day_mod = mod((B+day_no+4),7);
if day_mod == 0
day_mod = 7
end
month = {'January' 'February' 'March' 'April' 'May' 'June' 'July' 'August' 'September' 'October' 'December' 'November'};
day = {'Mon' 'Tue' 'Wed' 'Thu' 'Fri' 'Sat' 'Sun'};
if 0>A || 12<A || 0>B || (datenum(2016,A+1,1)-datenum(2016,A,1))<B
m = struct([])
else
a = month{A};
b = day{day_mod};
m = struct('month',a,'date', B ,'day',b);
end
end

カテゴリ

質問済み:

2017 年 11 月 7 日

コメント済み:

2019 年 1 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by