how to make a function that return cell array of the month

조회 수: 8 (최근 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
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
Muhammad Usman Saleem
Muhammad Usman Saleem 2015년 6월 7일
@Christos Vyzantios i have formatted your code but i am getting below mentioned error
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
error i get
Your solution is _not_ correct.
Please correct my code. Thanks in advance for that...

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

채택된 답변

Walter Roberson
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
Muhammad Usman Saleem
Muhammad Usman Saleem 2015년 6월 5일
@walter see my comment on @jan post please..

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

추가 답변 (3개)

Jan
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
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
Muhammad Usman Saleem
Muhammad Usman Saleem 2015년 6월 8일
@Walter , i have solved this yesterday..

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


abdo desoki
abdo desoki 2015년 6월 6일
change that variable A with m and it will run correctly .
  댓글 수: 1
Muhammad Usman Saleem
Muhammad Usman Saleem 2015년 6월 7일
@abdo , i use that but error remains same
function m = June2015
m=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
m(i,:) = {'June', i, 'DateName'} ;
end
end
that error is come the code
Your solution is _not_ correct.

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


Luxman Maheswaran
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 CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by