Use "find" function in "datetime"
59 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Walter Roberson
2023-10-18
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 个评论
更多回答(1 个)
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')
% Check
D(IDX,:)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!