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

조회 수: 10 (최근 30일)
Jingyu Yang
Jingyu Yang 2020년 11월 7일
댓글: Jingyu Yang 2020년 11월 15일
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