Use "find" function in "datetime"
59 views (last 30 days)
Show older comments
RG
on 18 Oct 2023
Answered: Sulaymon Eshkabilov
on 18 Oct 2023
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.
0 Comments
Accepted Answer
Walter Roberson
on 18 Oct 2023
T = [datetime(2016,2,18); datetime(2016, 2, 18, 0, 0, 0.1)]
id = find(T == '2016-02-18 00:00:00')
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'
T2 = dateshift(T, 'start', 'second')
id2 = find(T2 == '2016-02-18 00:00:00')
0 Comments
More Answers (1)
Sulaymon Eshkabilov
on 18 Oct 2023
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')
% Check
D(IDX,:)
0 Comments
See Also
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!