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

9 views (last 30 days)
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.

Accepted Answer

Image Analyst
Image Analyst on 19 Nov 2014
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 Comments
Jim Hamill
Jim Hamill on 20 Nov 2014
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

Sign in to comment.

More Answers (1)

Adam Danz
Adam Danz on 27 Jan 2020
Edited: Adam Danz on 27 Jan 2020
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 Comments
Image Analyst
Image Analyst on 27 Jan 2020
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 on 27 Jan 2020
Edited: Adam Danz on 27 Jan 2020
Thanks for pointing that out. I changed the inputs in my answer after copy-pasting the full example but forgot to update the results.

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!