MATLAB Answers

How can I change 'NaT' value to normal Date value?

19 views (last 30 days)
This is my example file. It's filename is 'data.txt'
<data.txt>
Date : 2020/8/25 . Tuesday . 20:0:10 . Distance = 1531 cm
Date : 2020/8/25 . Tuesday . 20:0:11 . Distance = 1531 cm
Date : 2020/8/25 . Tuesday . 20:0:12 . Distance = 1531 cm
Date : 2020/8/25 . Tuesday . 20:0:13 . Distance = 1532 cm
Date : 2020/8/25 . Tuesday . 20:0:14 . Distance = 1532 cm
Date : 2020/8/25 . Tuesday . 20:0:15 . Distance = 1530 cm
Date : 2020/8/25 . Tuesday . 20:0:16 . Distance = 1531 cm
Date : 2020/8/25 . Tuesday . 20:0:17 . Distance = 1531 cm
Date : 2020/8/25 . Tuesday . 20:0:18 . Distance = 1536 cm
Date : 2020/8/25 . Tuesday . 20:0:19 . Distance = 1560 cm
Date : 2020/8/25 . Tuesday . 20:0:20 . Distance = 1560 cm
Date : 2020/8/25 . Tuesday . 20:0:21 . Distance = 1378 cm
Date : 2020/8/25 . Tuesday . 20:0:22 . Distance = 1378 cm
Date : 2020/8/25 . Tuesday . 20:0:23 . Distance = 1103 cm
Date : 2020/8/25 . Tuesday . 20:0:24 . Distance = 2303 cm
Date : 2020/8/25 . Tuesday . 20:0:25 . Distance = 2441 cm
Date : 2020/8/25 . Tuesday . 20:0:26 . Distance = 1896 cm
Date : 2020/8/25 . Tuesday . 20:0:27 . Distance = 1483 cm
Date : 2020/8/25 . Tuesday . 20:0:28 . Distance = 1483 cm
Date : 2020/8/25 . Tuesday . 20:0:30 . Distance = 1175 cm
Date : 2020/8/25 . Tuesday . 20:0:31 . Distance = 1175 cm
I want to import it as a timetable.
So, I use these codes :
opts = detectImportOptions('data.txt');
opts.VariableNames = ["Va
opts = detectImportOptions('data.txt');
opts.VariableNames = ["Var1", "Var2", "Date", "Var4", "Day", "Var6", "Time", "Var8", "Var9", "Var10", "Distance", "Unit"];
opts.SelectedVariableNames = ["Date", "Time", "Distance"];
opts.VariableTypes = ["categorical", "char", "datetime", "char", "categorical", "char", "duration", "char", "categorical", "char", "double", "categorical"];
opts = setvaropts(opts,"Time","DurationFormat","hh:mm:ss.S");
opts = setvaropts(opts, "Time", "InputFormat", "YY/mm/dd")
Lidar = readtimetable('data.txt',opts); %라이다 엑셀 파일 넣어주기
Lidar.Date = Lidar.Date + Lidar.Time; % combine Date + Time data
Lidar = retime(Lidar,"secondly","fillwithconstant","Constant",0);
Lidar = removevars(Lidar,"Time");
But when I run this codes, It stops at this line :
Lidar.Date = Lidar.Date + Lidar.Time; % combine Date + Time data
And the resulting value is also output like this.
How can I get normal value like 2020.08.20, not NaT?
This is the sample. I want this format Timetable :
How can I create a timetable like this by modifying my upper original code?
  1 Comment
Walter Roberson
Walter Roberson on 7 Nov 2020
Set the Prefix option (for first column) to "Date : " perhaps?
I think you will need to adjust your variables as well

Sign in to comment.

Accepted Answer

Reshma Nerella
Reshma Nerella on 10 Nov 2020
Hi,
In this line of the code, you are setting InputFormat parameter for the variable 'Time' .
opts = setvaropts(opts, "Time", "InputFormat", "YY/mm/dd");
Instead use the InputFormat parameter for the variable 'Date', since it is in the the datetime format and specify valid value.
opts = setvaropts(opts, "Date", "InputFormat", "yyyy/MM/dd");
NaT will be replaced by the dates from the table.
For more information on setvaropts,refer to the documentation page: setvaropts

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by