How can I create a function that converts the number of a month to the name of a month?

4 vues (au cours des 30 derniers jours)
So far I have this.
curdateTime = clock;
Monthnumber=curdateTime(2);
Year=curdateTime(1);
Day=curdateTime(3);
Hour=curdateTime(4);
Minute=curdateTime(5);
Monthname=["January", "Febuary", "March", "April","May", "June", "July", "August", "September", "October", "November", "December"];
Month=Monthname(Monthnumber);
if Hour < 12
fprintf('Good morning, the time is %d minutes past %d o clock AM on %d %d %d.', Minute,Hour,Day,Month,Year)
else
hour=Hour-12;
fprintf ('Good afternoon, the time is %d minutes past %d o clock PM on %d %d %d.', Minute,hour,Day,Month,Year)
end
It gives me an error saying this
Good afternoon, the time is 47 minutes past 8 o clock PM on 22 Error using fprintf
Unable to convert 'string' value to 'int64'.
Error in Jamiesonlab3 (line 15)
fprintf ('Good afternoon, the time is %d minutes past %d o clock PM on %d %d %d.',
Minute,hour,Day,Month,Year)
Any help would be appreciated.

Réponses (2)

Adam Danz
Adam Danz le 22 Sep 2020
Modifié(e) : Adam Danz le 22 Sep 2020
No need to create a function for that.
Monthnumber = 4;
M = month(datetime(1, Monthnumber, 1), 'Name') % also try 'shortname'
% M =
% 1×1 cell array
% {'April'}
To apply that to fprintf,
fprintf('The month is %s\n',M{1})
% The month is April

John Jamieson
John Jamieson le 22 Sep 2020
Modifié(e) : John Jamieson le 22 Sep 2020
This question has been solved. I simply had to replace a %d where Month was located in the fprintf with %s. If I do that, the function works fine.

Catégories

En savoir plus sur Time Series Objects dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by