Subset using time range following specific time
3 views (last 30 days)
Show older comments
Louise Wilson
on 29 Jun 2021
Commented: Louise Wilson
on 1 Jul 2021
I have two tables (see attached screenshot.) Both tables have a list of datetimes. I want to select the value from counts_subset.BoatCount if the counts_subset.DateTime falls within any of dBtable.DateTime+2 minutes, and then I went to append that count to the same row of the matching datetime. I can do it with withtol, but withtol selects 2 minutes before and after, whereas I only want to select two minutes after. Is this possible?
tmatch_tt1=counts_subset(withtol(dBtable.DateTime,tol),:).DateTime; %using withtol
Update: I figured out by subsetting with withol(2 minutes) and then selecting only those datetimes which occur after 2 minutes. It would be good to know if there is a quicker/more efficient way though.
%select the times in table that match countd times with tolerance
tmatch_tt1=counts_subset(withtol(dBtable.DateTime,tol),:).DateTime;
%extract rows in table that are within 2 min
[Lia,Loc]=ismember(tmatch_tt1,counts_subset.DateTime)
counts_subset_new=counts_subset(Loc,:);
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');
%find datetimes which occur AFTER 2 mins
for ii=1:height(new_table)
%get wav datetime from .wav filename
split_name=strsplit(new_table.name{ii},{'.','_'});
wav_dt=split_name(2);
new_table.wavDateTime(ii)=datetime(wav_dt,'InputFormat','yyMMddHHmmss');
%get image datetime from img filename
split_imgname=strsplit(new_table.Filename_boatCounts{ii},{'-'});
img_dt=strcat(split_imgname(2),split_imgname(3));
new_table.imgDateTime(ii)=datetime(img_dt,'InputFormat','yyyyMMddHHmm');
end
%only select rows where img dt is after wav dt
S=new_table.imgDateTime>=new_table.wavDateTime;
new_table=new_table(S,:);
dBcalcs_CameraCounts.(sites{a})=new_table;
2 Comments
dpb
on 30 Jun 2021
Be much more helpful to attach the data tables as .mat files instead of just images...nobody can experiment with an image.
I'd think maybe rowfun() with diff() might be just as quick, but haven't tried to build a test dataset to compare.
I've not had opporunity to use withtol to date but looks like an optional two-vector of tolerance values of plus/minus tolerance values would be a good enhancement request, maybe...
Accepted Answer
Seth Furman
on 1 Jul 2021
Two alternative workflows are
- Shift the midpoint and tolerance passed into withtol to achieve the desired range.
- Use logical subscripting to only include those datetimes in the desired range.
For example
rng default
dt = datetime(2000,1,1)+minutes(0:5:50)'
posNeg = [-1 1];
posNeg = posNeg(randi(2,11,1))';
dtNoisy = dt+minutes(2.5).*rand(11,1).*posNeg;
tt = timetable(dtNoisy,(1:11)')
Shift the midpoint and tolerance passed into withtol to achieve the desired range
tt(withtol(dt+minutes(1),minutes(1)),:)
Use logical subscripting to only include those datetimes in the desired range
tt(minutes(0) <= tt.dtNoisy-dt & tt.dtNoisy-dt <= minutes(2),:)
More Answers (0)
See Also
Categories
Find more on Timetables 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!