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

11 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2020-11-7
Set the Prefix option (for first column) to "Date : " perhaps?
I think you will need to adjust your variables as well

请先登录,再进行评论。

采纳的回答

Reshma Nerella
Reshma Nerella 2020-11-10
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Timetables 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by