Function to use withtol but only forward in time?

조회 수: 1 (최근 30일)
Louise Wilson
Louise Wilson 2021년 3월 2일
답변: Zuber Khan 2024년 7월 17일
I have a timetable where the rows are datetimes. I then have a second timetable and I want to match the values in the second timetable to the first, but only where the times match within a certain time range. I want to include only those rows from the second timetable which occur within 2 minutes of the datetimes in the first. I can do this using withtol, but then I get + and - 2 minutes. How do I set the tolerance to only include 2 minutes after?
tol=minutes(2);
%we get a value for boat counts if photo was captured 2 minutes either side
%of hydrophone recording
for a=1:length(sites)
dBtable=dBcalcs.(sites{a});
counts_subset_rows=contains(boat_counts.Site(:),sites{a});
counts_subset=boat_counts(counts_subset_rows,:);
%select the times in boat_counts that match detections times with tolerance
tmatch_tt1=counts_subset(withtol(dBtable.DateTime,tol),:).DateTime;
%extract rows in boat_counts that are within 2 min of hydrophone recordings
[Lia,Loc]=ismember(tmatch_tt1,counts_subset.DateTime)
counts_subset_new=counts_subset(Loc,:);
%change detections datetimes to closest datetime of images
%(specific datetime of hydrophone recording remains in spectrogram filename
%of each row)
dBtable=table2timetable(dBtable);
tt2_matched=retime(dBtable,tmatch_tt1,'nearest');
%use synchronize to join the matched rows
new_table=synchronize(tt2_matched,counts_subset_new);
new_table.DateTime=datetime(new_table.DateNum,'ConvertFrom','datenum');
dBcalcs_CameraCounts.(sites{a})=new_table;
end

답변 (1개)

Zuber Khan
Zuber Khan 2024년 7월 17일
Hi,
To filter rows from the second timetable that occur within a specific time range (e.g., within 2 minutes after the times in the first timetable) without including times before, you can use logical indexing and the "withtol" function in combination with additional conditions.
You can follow the approach as mentioned below to achieve this:
  1. Use the "withtol" function to get all rows within the tolerance (both before and after the time).
  2. Filter out the rows that are not within the desired range.
Kindly refer to the following code for your reference:
tol = minutes(2);
%we get a value for boat counts if photo was captured 2 minutes either side
%of hydrophone recording
for a = 1:length(sites)
dBtable = dBcalcs.(sites{a});
counts_subset_rows = contains(boat_counts.Site(:), sites{a});
counts_subset = boat_counts(counts_subset_rows, :);
% Select the times in boat_counts that match detection times with tolerance
tmatch_tt1 = counts_subset(withtol(dBtable.DateTime, tol), :).DateTime;
% Filter out rows that are not within 0 to 2 minutes after the times in dBtable
valid_rows = tmatch_tt1 >= dBtable.DateTime & tmatch_tt1 <= dBtable.DateTime + minutes(2);
tmatch_tt1 = tmatch_tt1(valid_rows);
% Extract rows in boat_counts that are within 2 min of hydrophone recordings
[Lia, Loc] = ismember(tmatch_tt1, counts_subset.DateTime);
counts_subset_new = counts_subset(Loc, :);
% Change detections datetimes to closest datetime of images
% (specific datetime of hydrophone recording remains in spectrogram filename
% of each row)
dBtable = table2timetable(dBtable);
tt2_matched = retime(dBtable, tmatch_tt1, 'nearest');
% Use synchronize to join the matched rows
new_table = synchronize(tt2_matched, counts_subset_new);
new_table.DateTime = datetime(new_table.DateNum, 'ConvertFrom', 'datenum');
dBcalcs_CameraCounts.(sites{a}) = new_table;
end
I hope it will address your query.
Regards,
Zuber

카테고리

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

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by