필터 지우기
필터 지우기

Use "find" function in "datetime"

조회 수: 59 (최근 30일)
RG
RG 2023년 10월 18일
답변: Sulaymon Eshkabilov 2023년 10월 18일
I have some entries as datetime class. I am trying to get the id where the entry for date is "2016-02-18 00:00:00" for instance. I know I have the exact point there but the id is empty. It works for some other columns of datetime but for one of them does not work!
id=find(time=='2016-02-18 00:00:00')
id = 0×1 empty double column vector
I appreciate any help.

채택된 답변

Walter Roberson
Walter Roberson 2023년 10월 18일
T = [datetime(2016,2,18); datetime(2016, 2, 18, 0, 0, 0.1)]
T = 2×1 datetime array
18-Feb-2016 00:00:00 18-Feb-2016 00:00:00
id = find(T == '2016-02-18 00:00:00')
id = 1
Both entries display the same with the default format, but that does not mean they are equal.
T.Format = 'dd-MMM-yyyy HH:mm:ss.SSSSSS'
T = 2×1 datetime array
18-Feb-2016 00:00:00.000000 18-Feb-2016 00:00:00.100000
T2 = dateshift(T, 'start', 'second')
T2 = 2×1 datetime array
18-Feb-2016 00:00:00.000000 18-Feb-2016 00:00:00.000000
id2 = find(T2 == '2016-02-18 00:00:00')
id2 = 2×1
1 2

추가 답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 10월 18일
It looks like your imported data variable name may not be correctly assigned. Here is a plain example where datetime works ok:
%% Set up the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 2);
% Specify range and delimiter
opts.DataLines = [2, Inf];
opts.Delimiter = " ";
% Specify column Names and Types
opts.VariableNames = ["Time", "Data"];
opts.VariableTypes = ["datetime", "double"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
opts.ConsecutiveDelimitersRule = "join";
opts.LeadingDelimitersRule = "ignore";
% Specify variable properties
opts = setvaropts(opts, "Time", "InputFormat", "yyyy-MM-dd HH:mm:ss");
% Import the data
D = readtable("DATE_Data.txt", opts); % Note that the imported data is a table variable
IDX=find(D.Time=='2016-02-18 00:00:00')
IDX = 2×1
1 4
% Check
D(IDX,:)
ans = 2×2 table
Time Data ___________ _____ 18-Feb-2016 13 18-Feb-2016 23.23

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by