Seperate yyyymm to mm-yyyy

I uploaded a table with one of the columns showing yyyymm,
How do I get tha column in the mm-yyyy format

Respuestas (1)

Star Strider
Star Strider el 20 de En. de 2020

0 votos

Try this:
ym = [201910
201911
201912
202001]; % Numeric (?)
yms = compose('%6d',ym)
my = datetime(yms, 'InputFormat','yyyymm', 'Format','mm-yyyy')
producing:
my =
4×1 datetime array
10-2019
11-2019
12-2019
01-2020

6 comentarios

Megan Mirkhanian
Megan Mirkhanian el 20 de En. de 2020
so my file that I uploaded is called Data
and Data includes three columns, the first column has the yyyymm format. How do I tell matlab to only work with column 1?
Star Strider
Star Strider el 20 de En. de 2020
It would help to have your ‘Data’ file.
That aside, (and assuming that ‘Data(:,1)’ is numeric, since that sstill has not been revealed), try this:
yms = compose('%6d',Data(",1));
my = datetime(yms, 'InputFormat','yyyymm', 'Format','mm-yyyy');
That should work, and also will produce a datetime array.
Megan Mirkhanian
Megan Mirkhanian el 20 de En. de 2020
This is how the table looks
thank you!
Data.Var1 = datetime(Data.Var1, 'InputFormat', 'yyyymm', 'Format', 'mm-yyyy');
Star Strider
Star Strider el 21 de En. de 2020
@Walter — Thank you!
Stephen23
Stephen23 el 21 de En. de 2020
Editada: Stephen23 el 21 de En. de 2020
Note that for datetime objects lowercase mm represents minutes and uppercase MM represents months:
It is very unlikely that the data really encoded a timestamp of "yearminute" which should then be displayed as "minute-year". Because the datetime format capitalization is not very intuitiive, it is worth reading/checking the datetime documentation every time, to easily avoid these kind of bugs.

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 20 de En. de 2020

Editada:

el 21 de En. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by