Split date variable of a table into 3 variables year month and day

4 vues (au cours des 30 derniers jours)
Thomas Vescovini
Thomas Vescovini le 16 Juil 2019
Commenté : Adam Danz le 18 Juil 2019
Hi, I have table with 3 variables (date, latitude, longitude) where the first variable is the date (dd/mm/yyyy) and I want the create a table with 5 variables with the following schema :
year month day latitude longitude
2002 01 01 xxx xxx
I didn't find out how to use datenum and datevec the right way...
Thank you,
TV

Réponse acceptée

Adam Danz
Adam Danz le 17 Juil 2019
"I have table with 3 variables (date, latitude, longitude) where the first variable is the date (dd/mm/yyyy)"
% Create example table
T = table(datestr(round(now()-(0:5))','dd/mm/yyyy'),rand(6,1)*100, rand(6,1)*100, ...
'VariableName', {'date','latitude','longitude'});
"I want the create a table with 5 variables with the following schema..."
% parse dates
[y,m,d] = datevec(T.date, 'dd/mm/yyyy');
Tdate = table(y,m,d,'VariableName',{'year','month','day'});
% Create new table
Tnew = [Tdate,T(:,{'latitude','longitude'})];
Result
Tnew =
6×5 table
year month day latitude longitude
____ _____ ___ ________ _________
2019 7 18 84.565 40.058
2019 7 17 44.594 91.371
2019 7 16 54.392 67.381
2019 7 15 75.07 39.048
2019 7 14 89.192 41.376
2019 7 13 58.357 70.361
  2 commentaires
Thomas Vescovini
Thomas Vescovini le 18 Juil 2019
Thanks, that works perfectly!
Adam Danz
Adam Danz le 18 Juil 2019
Glad I could help!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Produits


Version

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by