필터 지우기
필터 지우기

How to omit milliseconds from a table.

조회 수: 1 (최근 30일)
Constantin
Constantin 2023년 11월 6일
댓글: Constantin 2023년 11월 6일
Omitting milliseconds when importing datetime.
Hello everyone, I am very new to matlab and writing scripts so please be patient with me.
I am trying to omit the milliseconds from the time I am importing from a CVS file. I came up with the below code to import cvs files based on a naming prefix into a table, but "DatetimeFormate" only changes the displaying of the values not how it is stored.
Ultimately I want to filter out rows from the table with the same HH:mm:ss and join this table with a second one based on the time column. I tried to filter for unique values, or calculating the mean of the values with the same time, but every entry is kept because of the milliseconds being different, so I am looking for a way to omit them.
fSearch=fullfile('TC*'); % build matching wildcard expression
d_TC = dir(fSearch); % and look for match
opts_TC = detectImportOptions(d_TC.name,"VariableNamingRule","preserve"); %Preserving Column Names
opts_TC = setvaropts(opts_TC,"Time","Type","datetime",DatetimeFormat="HH:mm:ss"); %InputFormat="MM/dd/yyyy HH:mm:ss.SSS"
TC = readtable(d_TC.name,opts_TC);
The Resulting table is 90394x6 with the first column being datetime Values like "'11:52:54" and the following five double values for temperatures from a sensor.
Thank you!

채택된 답변

Walter Roberson
Walter Roberson 2023년 11월 6일
TC.Time = dateshift(TC.Time, 'start', 'second');
  댓글 수: 2
Les Beckham
Les Beckham 2023년 11월 6일
FYI, you might want to add the 'nearest' rule argument to specify rounding instead of truncation:
TC.Time = datetime(2023, 11, 6, 2, 49, 58.8)
TC = struct with fields:
Time: 06-Nov-2023 02:49:58
TC.TimeTruncated = dateshift(TC.Time, 'start', 'second')
TC = struct with fields:
Time: 06-Nov-2023 02:49:58 TimeTruncated: 06-Nov-2023 02:49:58
TC.TimeRounded = dateshift(TC.Time, 'start', 'second', 'nearest')
TC = struct with fields:
Time: 06-Nov-2023 02:49:58 TimeTruncated: 06-Nov-2023 02:49:58 TimeRounded: 06-Nov-2023 02:49:59
Constantin
Constantin 2023년 11월 6일
Thank you! This is exactly what I needed. I did end up going with the rounded time, as that will be more accurate when I merge it with the other table.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by