First occurance of data in timetable
7 views (last 30 days)
Show older comments
Hi and thanks in advance.
I have a timetable with windspeed data collected 6 hrly on different days of the month for the past 20 years (not continous). The timetable has 3 columns (date/time, ID, windspeed) for each ID there are 5-20 data points. For each ID I want to find the first occurance of windspeed > 17.5 and delete everything above. Simialry I want to find the last occurnace of windspeed > 17.5 and delete everything below.
FOr example, for the ID 3456, windspeed values are 10 12 18 16 12 20 15
After analysis i want the values as 18 16 12 20 along with the timestamps.
Thanks Once Again
0 Comments
Accepted Answer
Rishi
on 4 Jan 2024
Hi Sarvesh,
I understand that you want to write a code such that it gets rid of the occurrences which have windspeeds less than 17.5 before and after the first and last occurrences of windspeed greater than 17.5, respectively, for every ID.
Here is the code for the same:
% Assuming 'tt' is your timetable with variables 'DateTime', 'ID', and 'Windspeed'
DateTime = datetime({'2015-12-18 08:03:05';'2015-12-18 10:03:17';'2015-12-18 12:03:13';'2015-12-18 12:03:15';'2015-12-18 13:03:05';'2015-12-18 14:03:05';'2015-12-18 15:03:05';'2015-12-18 16:03:05';'2015-12-18 17:03:05';'2015-12-18 18:03:05'});
ID = [1; 2; 1; 2; 1; 2; 1; 1; 2; 2];
Windspeed = [10; 19; 18; 11; 16; 22; 20; 15; 13; 11];
tt = timetable(DateTime,ID,Windspeed);
uniqueIDs = unique(tt.ID);
trimmedTimetable = timetable();
for i = 1:length(uniqueIDs)
currentSubset = tt(tt.ID == uniqueIDs(i), :);
startIndex = find(currentSubset.Windspeed > 17.5, 1, 'first');
endIndex = find(currentSubset.Windspeed > 17.5, 1, 'last');
if ~isempty(startIndex) && ~isempty(endIndex)
currentSubset = currentSubset(startIndex:endIndex, :);
trimmedTimetable = [trimmedTimetable; currentSubset];
end
end
% Sort the trimmed timetable by DateTime if necessary
trimmedTimetable = sortrows(trimmedTimetable, 'DateTime');
disp(trimmedTimetable);
Hope this helps!
More Answers (0)
See Also
Categories
Find more on Detection 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!