how do I change numbers to dates?

1 view (last 30 days)
So I'm pretty knew to matlab and I'm trying to take a column of data that is filled with numbers between 1-12 and change them into their respective months. Is that even posible and if so, how? Thank you! :)

Accepted Answer

Image Analyst
Image Analyst on 14 Oct 2021
Use the datetime() function and the month() function:
monthNumberArray = 1:12; % Vector of whatever months you want.
t = datetime(2021, monthNumberArray, 1)
t = 1×12 datetime array
01-Jan-2021 01-Feb-2021 01-Mar-2021 01-Apr-2021 01-May-2021 01-Jun-2021 01-Jul-2021 01-Aug-2021 01-Sep-2021 01-Oct-2021 01-Nov-2021 01-Dec-2021
m = month(t, 'name')
m = 1×12 cell array
{'January'} {'February'} {'March'} {'April'} {'May'} {'June'} {'July'} {'August'} {'September'} {'October'} {'November'} {'December'}
monthNumberArray = randi([1 12], 1, 5); % Vector of whatever months you want.
t = datetime(2021, monthNumberArray, 1)
t = 1×5 datetime array
01-Jun-2021 01-Jan-2021 01-Mar-2021 01-Jul-2021 01-Feb-2021
m = month(t, 'name')
m = 1×5 cell array
{'June'} {'January'} {'March'} {'July'} {'February'}
  2 Comments
Image Analyst
Image Analyst on 14 Oct 2021
If the Answer works, you can click the "Accept this answer" link to award the answerer "reputation points." Thanks in advance.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!