how to make function that return struct vector whose elements are days of month

Hi every one; I am going to attempt that query: a function called May2015 that returns a struct vector (row or column) whose elements correspond to the days of May, 2015. Each struct should contain three fields with these (exact) field names: “month”, “date”, and “day” (all lower case). • The month field must contain the string 'May' (uppercase ‘M’). • The date field must contain a scalar of type double that equals the date (1 through 31). • 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 eleventh element of the struct array that is returned by the function:
>> m = May2015;
>> m(11)
ans =
month: 'May'
date: 11
day: 'Mon'
I am using that code
function M=May2015(a)
M=strut('month','May','date',a)
end
I have no idea how i can set the day like mon,tues,wed,thus,fri sat,sun, corresponding date no. May month start from 1 to 30 and the day on 1 was mon and of 30 was tues. Kindly guide me how can i mange day (string) with date. I think i have to use for loop but not idea.. Thanks in advance

댓글 수: 2

Is this homework? If so, can you add 'homework' as a tag?
how to write the name of month also(for example 2 as input will give an output of February)

댓글을 달려면 로그인하십시오.

 채택된 답변

Augusto Oliveira
Augusto Oliveira 2015년 6월 1일
It's m = May2015, not m = May2015(n)... i = 1:31

댓글 수: 2

function m = May2015
for i = 1:31
[DateNumber, DateName] = weekday(datenum([2015 5 i]));
m(i) = struct('month','May','date',i,'day', DateName);
end
@Augusto thanks for contributions. Why we not set input argument n to May2015??

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Andrei Bobrov
Andrei Bobrov 2015년 6월 1일
편집: Andrei Bobrov 2015년 6월 2일
function out = May2015
[~,k] = weekday(datenum(2015, 5, (1:31)'));
out = struct('month','May','date',num2cell((1:31)'),'day',cellstr(k));
use:
>> m = May2015
m =
31x1 struct array containing the fields:
month
date
day
>> m(11)
ans =
scalar structure containing the fields:
month = May
date = 11
day = Mon
>>

댓글 수: 6

@Andrei thanks for contributions.. i use that code
function out = May2015(n)
[~,k] = weekday(datenum([2015 5 n]));
out = struct('month','May','date',n,'day',k);
end
and in testing getting that error
Your solution is _not_ correct.
Muhammad, the answer of Andrei is not missing a lot. If you want to have a struct array, you can either use looping on Andrei's solution, i.e.,
for i=1:length(n)
out(i) = struct('month', 'May', 'date', n(i), 'day', k(i));
end
or use cell arrays for initialization
nc = num2cell(n);
out = struct( ...
'month', 'May', ...
'date', nc, ...
'day', k);
Titus
@Titus i am revolving around the question.but not finding the solution??
in 2nd line what is the purpose of (~) symbol.

댓글을 달려면 로그인하십시오.

Hi Muhammad,
a hint:
doc weekday
Titus

댓글 수: 1

@Titus thanks for contributions.. I very new to programming ... create more easily solution for me...

댓글을 달려면 로그인하십시오.

charu sharma
charu sharma 2015년 8월 20일
you need to mention the number of dates in MAY that is 1 to 31 and days Here is the more simple solution for you: http://farzicoders.blogspot.in/2015/08/write-function-called-may2015-that.html

카테고리

도움말 센터File Exchange에서 Data Type Identification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by