# Creating a Loop to Apply a Certain Constant Based on the Month in the Time Series

1 view (last 30 days)
Luke Travis on 7 Nov 2022
Answered: JAT on 17 Mar 2023
I have five months of 6-minute water level (WL) data. I am inputing the WL data into this equation to derive a pressure value:
Pressure = (Gravity * Density * WL) / 1000
Gravity is a constant that will remain the same over the five months. My issue is that the density changes from month to month with the following values:
Density1 = 1.01;
Density2 = 1.019;
Density3 = 1.02;
Density4 = 1.02;
Density5 = 1.017;
With my time series of WL data, I am trying to create a loop that will be able to detect which month the data was recorded in and apply that specific density value to the pressure equation. The reason I am trying to create this loop is because I will eventaully be working with multiple years worth of data. So for example I will have 12 different density values for each month of the year and If I have 4 years of data I will need the loop to loop back around after December and into the next year. If any other information is needed to understand what I am looking for, I am happy to provide more details.
Any help or advice is greatly appreciated!
Thank you
##### 2 CommentsShow 1 older commentHide 1 older comment
Lei Hou on 18 Nov 2022
Use datetime('now') returns the current time. Then call month(datetime) to get the month information.
>> datetime('now')
ans =
datetime
18-Nov-2022 11:41:23
>> month(datetime('now'))
ans =
11

Santosh Fatale on 14 Nov 2022
Hi Luke,
If you are using timeseries objects for data processing, then please note that timeseries will no longer be supported in a future release. Instead of timeseries objects, you can use timetable.
To convert your timeseries data into timetable format, you can use the timeseries2timetable function.
I assumed that the "Density" changes with the month but not with the year. If you have timeseries data for "WL", you can utilize the following steps to create a loop over the variable "WL":
• Convert timeseries WL data into timetable format.
timetableWL = timeseries2timetable(WL)
• For each entry in WL, you can get information about the month using the function month. The function "month" needs input of type datetime format, which you can retrieve using the following commands from WL timetable.
datetimeData = timetableWL.Properties.RowTimes
• For each month retrieved in the last step, assign a known value of density from lookup table.

JAT on 17 Mar 2023
Hi Luke, this is an old question but I came across it because I'm looking for a similar answer. I am trying to add a fixed monthly value (so 12 different values) to all the Januarys, February's, etc in a multi-year time series. So I want Matlab to go through and 'know' when it's January and add amount 1, and then 'know' when it's February and add amount 2 - regardless of the day and year. Did you ever solve this question?

### Categories

Find more on Dates and Time 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!

Translated by