How do I write a function to convert numerical month of the year to the name of that month?

5 次查看(过去 30 天)
I am knew to matlab and am required to write and test a function that converts numerical month of the year to the name of that month(Assuming 1=January to 12=December). I have to do 3 variations of this, one with "if", one with "else if" and the other with "switch". I would just like to know which function would be suitable to perform this operation.

采纳的回答

Image Analyst
Image Analyst 2014-11-19
Hints (probably too much of a hint):
months = {'January', 'February', 'March', 'April', 'May', 'June', ...
'July', 'August', 'September', 'October', 'November', 'December'};
monthNumber = 3; % Whatever .. use inputdlg() to ask user.
if monthNumber > 1 && monthNumber < length(months)
theMonth = months{monthNumber}
end
switch monthNumber
case {1,2,3,............. You finish it!
fprintf('You picked month %s\n', months..... You finish it!
otherwise
fprintf('Invalid month number of %d', monthNumber);
end
You're going to have to finish/fix it and add the elseif to the "if" case to alert user to the invalid month. If you want to do it with 11 elseif's and check every number from 1 to 12 individually, you can do that, but it's really completely unnecessary since you can get the month with a simple index into the months array.
  2 个评论
Jim Hamill
Jim Hamill 2014-11-20
I have given this a go but still do not think I have quite got it. I have to then Write a test script called TestMonths which uses a for loop, to display all the names from each function month2nameA, month2nameB, month2nameC.
The name of each month should appear together 3 times (I.e. once for each
function, then move to the next month in the list/loop). I have written my first function as follows:
function [months] = month2nameA (MonthNumber)
months = {'January', 'February', 'March', 'April', 'May', 'June', ...
'July', 'August', 'September', 'October', 'November', 'December'};
monthNumber = 3; %I'm not sure how to use inputdlg() correctly, is there
%another way of prompting the number or is this the only way
if monthNumber > 1 && monthNumber < length(months)
theMonth = months{monthNumber}
end
My second function is:
function [months] = month2nameB (MonthNumber)
months = {'January', 'February', 'March', 'April', 'May', 'June', ...
'July', 'August', 'September', 'October', 'November', 'December'};
monthNumber = ;
if monthNumber > 1 && monthNumber < length(months)
theMonth = months{monthNumber};
elseif monthNumber == 1
theMonth = 'January';
elseif monthNumber == 12
theMonth = 'December';
else
theMonth = 'Invalid Month Number'
end
end
I assumed the elseif functions would be for returning the correct number for 1 and 12 as the monthNumber is between 1 and 12 but do not think this is the correct way to display it
The third function I am still very unsure about, I know it is not correct but is it somewhere along the lines of this:
function [months] = month2namec (MonthNumber)
switch (monthNumber);
case {1,2,3,4,5,6,7,8,9,10,11,12};
fprintf('You picked month %s\n', months{'January', 'February'...
'March', 'April', 'May', 'June', 'July', 'August', 'September'...
'October', 'November', 'December'});
otherwise
fprintf('Invalid month number of %d', monthNumber);
end
end
Any help with this would be greatly appreciated, thanks

请先登录,再进行评论。

更多回答(1 个)

Adam Danz
Adam Danz 2020-1-27
编辑:Adam Danz 2020-1-27
Another method to return month names given month numbers using datetime and month,
monthNames = month(datetime(1, 1:6, 1), 's')
% month numbers go here ^^^^
% monthNames =
% 1×6 cell array
% {'Jan'} {'Feb'} {'Mar'} {'Apr'} {'May'} {'Jun'}
  2 个评论
Image Analyst
Image Analyst 2020-1-27
Nifty trick. However I get them in a different order than you. I get the expected order:
monthNames =
1×6 cell array
{'Jan'} {'Feb'} {'Mar'} {'Apr'} {'May'} {'Jun'}
Adam Danz
Adam Danz 2020-1-27
编辑:Adam Danz 2020-1-27
Thanks for pointing that out. I changed the inputs in my answer after copy-pasting the full example but forgot to update the results.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Voronoi Diagram 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by