how to make a function that return cell array of the month
15 次查看(过去 30 天)
显示 更早的评论
Hi every one;
I am going to attempt that query:
Write a function called June2015 that returns a cell array of dimensions 30-by-3, whose rows correspond to the days of June, 2015. The three elements of each row must be set as follows:
• The first element refers to the string 'June' (uppercase ‘J’). • The second element refers to a scalar of type double that equals the date (1 through 30). • The third element refers to 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 cell array that is returned by the function:
>> m = June2015;
>> m(11,:)
ans =
'June' [11] 'Thu'
I am using that code
function m = June2015
A=cell(30,3)%declar cell array of 30 by 3
for i = 1:30
[DateNumber, DateName] = weekday(datenum([2015 6 i]));% making the value of DateNume for DateNumber
%and loop run number of rows in cell array
for j=1:3% loop run for column of cell array
A(i,:)={'June', i, 'DateName'}
end
end
end
but in testing i am getting that error
Your solution is _not_ correct.
Guide me about my corrections.. thanks in advance
8 个评论
Christos Vyzantios
2015-6-5
编辑:Walter Roberson
2015-6-8
I transform liitle the code but the problem exist
function m = June2015 %#ok<STOUT>
A=cell(30,3);%declar cell array of 30 by 3
for i = 1:30 %#ok<ALIGN>
[~, DateName] = weekday(datenum([2015 6 i]));
A(i,:) = {'June', i, DateName};
end
end
采纳的回答
Walter Roberson
2015-6-2
In your line
A(i,:)={'June', i, 'DateName'}
that would attempt to put the literal string 'DateName' into the array, rather than trying to put any content from the variable DateName into the array.
You probably want to select a portion of DateName rather than the whole thing.
更多回答(3 个)
Jan
2015-6-2
The loop over j is not required. Simply omit it.
The 3rd column of A should not be the string 'DateName(i,:)', but the contents of the variable DateName. So use:
A(i,:) = {'June', i, DateName} ;
7 个评论
Walter Roberson
2015-6-8
编辑:Walter Roberson
2015-6-8
function A = June2015
A=cell(30,3)%declar cell array of 30 by 3
for i = 1:30
[DateNumber, DateName] = weekday(datenum([2015 6 i]));
% making the value of DateNume for DateNumber and loop run number of rows in cell array
[A{i,:}] = deal('June', i, DateName) ;
end
end
Luxman Maheswaran
2015-6-8
You have to convert the number of DateName to a string such as 'Thu' I have checked it with a grader and it is correct
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Identification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!