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

3 views (last 30 days)
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

Accepted Answer

Adam Danz
Adam Danz on 17 Jul 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

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!