Matlab Make a function, called \"hmwk5.m\", to convert the month from a number t
ID: 3752932 • Letter: M
Question
Matlab
Make a function, called "hmwk5.m", to convert the month from a number to a string. It should have one input, an integer value, that specifies the month, i.e. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, or 12. It should return one output, a string, i.e. January, February, March, April, ..., or December. Use a "switch" statement to choose the output string. Note that the function should normally not print anything, unless the input is a value not listed above. In that case, it should print a message using the "error" command.
Like earlier assignments, use the "type" command and several test cases to show that your function works. Turn in a copy of the interaction, i.e. copy from the MATLAB command window, or use the diary command.
Example:
>> monthname = hmwk5(1);
>> disp(sprintf(' The month is %s.', monthname));
The month is January.
Explanation / Answer
Solution:
function [ monthname] = hmwk5(n)
monthname = ''";
switch n
case 1
monthname = 'January';
case 2
monthname = 'February';
case 3
monthname = 'March';
case 4
monthname = 'April';
case 5
monthname = 'May';
case 6
monthname = 'June';
case 7
monthname = 'July';
case 8
monthname = 'August';
case 9
monthname = 'September';
case 10
monthname = 'October';
case 11
monthname = 'November';
case 12
monthname = 'December';
otherwise
disp('Wrong number of input arguments')
end
end
Output:
>> monthname = hmwk5(1);
>> disp(sprintf(' The month is %s.', monthname));
The month is January.
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)