Convert matrix with 3 columns ([day month year]) into one string dd/mm/yyyy with each part taking value of one column

7 Ansichten (letzte 30 Tage)
Hello everybody,
I have a matrix Nx3 called Date with 3 columns describing "random" dates. First column is day, second is month and third is year, like this :
1 3 1981
28 3 1982
23 3 1983
9 5 1984
27 3 1985
15 2 1986
19 2 1987
16 4 1988
20 3 1989
etc.
I want to write a vector with one single column that has the form :
01/03/1981
28/03/1982
23/03/1983
etc.
I tried to use the datestr() function like this :
datestr(Date,'dd/mm/yyyy')
but it returns me a vector of length Nx3 (i.e. size 3Nx1) :
'01/01/0000'
'28/01/0000'
'23/01/0000'
'09/01/0000'
'27/01/0000'
'15/01/0000'
'19/01/0000'
'16/01/0000'
'20/01/0000'
'02/01/0000'
etc.
I do not understand what is wrong with datestr().
Is there another function that would put all the 3 columns in one, and adding a zero in front of the numbers of days or months with only one digit (i.e. also write 1 as 01) ?
Cheers

Akzeptierte Antwort

Arif Hoq
Arif Hoq am 15 Apr. 2022
Bearbeitet: Arif Hoq am 15 Apr. 2022
try this:
A=[1 3 1981
28 3 1982
23 3 1983
9 5 1984
27 3 1985
15 2 1986
19 2 1987
16 4 1988
20 3 1989];
T=table(A(:,1),A(:,2),A(:,3));
B=string(A);
C=strcat(B(:,1),'/',B(:,2),'/',B(:,3));
D=datetime(C,'format','dd/M/yyyy')
D = 9×1 datetime array
01/3/1981 28/3/1982 23/3/1983 09/5/1984 27/3/1985 15/2/1986 19/2/1987 16/4/1988 20/3/1989

Weitere Antworten (0)

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by