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

조회 수: 11 (최근 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에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by