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

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 件のコメント

Walter Roberson
Walter Roberson 2015 年 6 月 2 日
A(i,:)={'June', i, DateName(i,:)}
Muhammad Usman Saleem
Muhammad Usman Saleem 2015 年 6 月 2 日
@walter i use that:
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(i,:)'}
end
end
end
but error is same???
Walter Roberson
Walter Roberson 2015 年 6 月 2 日
'DateName(i,:)' means to put in the literal string 'DateName(i,:)', not what is contained in DateName(i,:)
Oh I see, I thought you were pre-calculating all of the date names. In that case, just
A(i,:) = {'June', i, DateName};
and your "for j" loop is useless
Christos Vyzantios
Christos Vyzantios 2015 年 6 月 4 日
編集済み: Walter Roberson 2015 年 6 月 4 日
I made some small changes but the grader didnt accept :(
function m = June2015 %#ok<STOUT>
A=cell(30,3);
for i = 1:30
[~, DateName] = weekday(datenum([2015 6 i]));% making the value of DateNume for DateNumber
%and loop run number of rows in cell array
A(i,:) = {'June', i, DateName};
end
end
Walter Roberson
Walter Roberson 2015 年 6 月 4 日
Your "function" defines the output as going to the variable named "m" but you never assign anything to that variable.
Muhammad Usman Saleem
Muhammad Usman Saleem 2015 年 6 月 5 日
@walter see my comment below...
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 日

0 投票

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 月 2 日
@Walter thanks for contributions.. I do not getting your guide. I only understand , you are saying me about correction of 'DateNume', but when i used without quote i still gets same error. looking for more understanding of your assistance
Muhammad Usman Saleem
Muhammad Usman Saleem 2015 年 6 月 2 日
@Walter???????
Muhammad Usman Saleem
Muhammad Usman Saleem 2015 年 6 月 5 日
@walter see my comment on @jan post please..

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

その他の回答 (3 件)

Jan
Jan 2015 年 6 月 2 日

0 投票

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 件のコメント

Muhammad Usman Saleem
Muhammad Usman Saleem 2015 年 6 月 5 日
@Jan Simon and @walter last time when i test my code it was correct but now when i again testing my code its shown an error. That code i was using
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
A(i,:) = {'June', i, 'DateName'} ;
end
end
guide me as my paper is tomorrow. Thanks in advance for assistance
Muhammad Usman Saleem
Muhammad Usman Saleem 2015 年 6 月 5 日
if i use
A(i,:) = {'June', i, DateName} ;
then again error is same....
Jan
Jan 2015 年 6 月 6 日
Then please be so kind and post, which error message you see. The code runs fine on my machine.
Muhammad Usman Saleem
Muhammad Usman Saleem 2015 年 6 月 7 日
@Jan thanks for contributions. I am checking that code in a grader.. which has output
Your solution is _not_ correct.
We are doing some major mistake in the code?
Muhammad Usman Saleem
Muhammad Usman Saleem 2015 年 6 月 7 日
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,:) = {'June', i, 'DateName'} ;
end
end
that code i using...
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 日

0 投票

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 日

0 投票

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

1 件のコメント

Walter Roberson
Walter Roberson 2015 年 6 月 8 日
The second output of weekday() is a string.

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

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by