For loop on timetable

2 Ansichten (letzte 30 Tage)
Priya Joshi
Priya Joshi am 4 Nov. 2021
Kommentiert: Priya Joshi am 4 Nov. 2021
Hello,
So I have hourly data and rain in mm. I want to make a for loop that checks if the rainfall is equal to 0 or greater than 1. If it is 0 for 3 consecutive hours then the time is added and labelled as "Dry Event". If the rain is greater than 1mm along with consecutive values, the those values nad the times are added is labelled as "rain event".
This is my code uptil now:
rf = readtimetable('C:\Users\Priya\Downloads\Thesis\Sydney Rain 1994.csv');
dailyRF = retime(rf,"daily","sum");
dt=hours(1);
event= retime(dailyRF,"regular","sum", "TimeStep",dt);
I am trying to attempt the below for loop, which I know is wrong because it keeps giving me errors. I tried using groupstats but I couldn't get the right answer. I am new to Matlab and the code does have basic errors, I know.
i = 1:size(event)
if event.RainfallMm_6Minutes==0
sum(event.Date)
'Dry Event'
elseif event.RainfallMm_6Minutes >= 1
sum(event.Date)
'Rain Event'
end
Thank you!!

Antworten (1)

KSSV
KSSV am 4 Nov. 2021
rf = readtimetable('Sydney Rain 1994.csv');
dailyRF = retime(rf,"daily","sum");
dt=hours(1);
event= retime(dailyRF,"regular","sum", "TimeStep",dt);
T = timetable2table(event) ;
t = T.(1) ;
R = T.(2) ;
% Arrange three time steps as moving window (the belo will give indices)
idx = bsxfun(@plus, (1 : 3), (0 : numel(R) - 3).');
iwant = sum(R(idx),2) ; % this will give you sum of three consecutive time steps
t = t(idx) ;
% find when consecutive days is zero
id1 = iwant==0 ; % logical indices
id2 = iwant > 0 ; % logical indices
t(id1,1) % this will give time step of first day where rainfall is zero three continuous steps
  1 Kommentar
Priya Joshi
Priya Joshi am 4 Nov. 2021
Im sorry maybe I explained it wrong. 3 consecutive data was just an example. Basially, the loop needs to check if the value is 0 or greater than 1. If consecutive values are 0 then add the time and label it as dry event, if the consecutive value is not 0 and is greater than 1 count how many such values exist and add the time for it.
For example, in the graph attached....all the consecutive hourly rain data that occur one after the other needs to be grouped as a single rain event. And all the consecutive empty or 0 rainfall data needs to be grouped together as dry events.
I just can't figure out how to do a loop for it. Im having a lot of errors when I write the code for it.
I used groupstats but it gave me a count for all the 0 and other values present in the table. That is not useful for me. I need to figure out when did the dry and rain events occurr and how long was the duration.
Thank you.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Cell Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by