How to create a time serie with daily data

4 views (last 30 days)
Please i am trying to make a time series with daily financial datas.
The datas are observed daily except on weekends.
My main concern is that i am trying to set a moving starting (02 of january 2017) date with the function dattetime but i am having an error message. Please could you tell me what i did wrong?
M
Also , can you tell me how i can account for the weekly two days break in the time series.
Regards
  1 Comment
Sandeep Mishra
Sandeep Mishra on 17 Jan 2025
Can you also attach the missing variable/function 'txt' and 'ts' for better debugging?

Sign in to comment.

Accepted Answer

Seth Furman
Seth Furman on 17 Jan 2025
Use isweekend to remove weekends from your datetime series.
You'll also want to make sure you use caldays (calendar days) instead of days (fixed-length days) when constructing your datetime series.
% Create datetime series
startDate = datetime(2017,1,2,Format="eeee uuuu-MM-dd")
startDate = datetime
Monday 2017-01-02
dt = (startDate:caldays(1):startDate+caldays(30))'
dt = 31x1 datetime array
Monday 2017-01-02 Tuesday 2017-01-03 Wednesday 2017-01-04 Thursday 2017-01-05 Friday 2017-01-06 Saturday 2017-01-07 Sunday 2017-01-08 Monday 2017-01-09 Tuesday 2017-01-10 Wednesday 2017-01-11 Thursday 2017-01-12 Friday 2017-01-13 Saturday 2017-01-14 Sunday 2017-01-15 Monday 2017-01-16 Tuesday 2017-01-17 Wednesday 2017-01-18 Thursday 2017-01-19 Friday 2017-01-20 Saturday 2017-01-21 Sunday 2017-01-22 Monday 2017-01-23 Tuesday 2017-01-24 Wednesday 2017-01-25 Thursday 2017-01-26 Friday 2017-01-27 Saturday 2017-01-28 Sunday 2017-01-29 Monday 2017-01-30 Tuesday 2017-01-31
% Remove weekends from datetime series
dt(isweekend(dt)) = []
dt = 23x1 datetime array
Monday 2017-01-02 Tuesday 2017-01-03 Wednesday 2017-01-04 Thursday 2017-01-05 Friday 2017-01-06 Monday 2017-01-09 Tuesday 2017-01-10 Wednesday 2017-01-11 Thursday 2017-01-12 Friday 2017-01-13 Monday 2017-01-16 Tuesday 2017-01-17 Wednesday 2017-01-18 Thursday 2017-01-19 Friday 2017-01-20 Monday 2017-01-23 Tuesday 2017-01-24 Wednesday 2017-01-25 Thursday 2017-01-26 Friday 2017-01-27 Monday 2017-01-30 Tuesday 2017-01-31 Wednesday 2017-02-01
% Same result displayed in French
string(dt,"eeee uuuu-MM-dd","fr_FR")
ans = 23x1 string array
"lundi 2017-01-02" "mardi 2017-01-03" "mercredi 2017-01-04" "jeudi 2017-01-05" "vendredi 2017-01-06" "lundi 2017-01-09" "mardi 2017-01-10" "mercredi 2017-01-11" "jeudi 2017-01-12" "vendredi 2017-01-13" "lundi 2017-01-16" "mardi 2017-01-17" "mercredi 2017-01-18" "jeudi 2017-01-19" "vendredi 2017-01-20" "lundi 2017-01-23" "mardi 2017-01-24" "mercredi 2017-01-25" "jeudi 2017-01-26" "vendredi 2017-01-27" "lundi 2017-01-30" "mardi 2017-01-31" "mercredi 2017-02-01"
Aside: Creating datetimes from localized text timestamps
I noticed that your comment says % start='02-janv-2017'. It's also worth noting that you can create datetimes from localized text timestamps. It's a good idea to specify the Locale Name-Value pair when doing this, so that your code will run on machines running MATLAB in different locales.
datetime("02-janv-2017",Locale="fr_FR")
ans = datetime
02-Jan-2017

More Answers (1)

Sandeep Mishra
Sandeep Mishra on 17 Jan 2025
Hi Job1003,
I noticed that you are encountering an error message related to the use of the 'strrep' function in MATLAB
The root cause of the issue arises from passing an incorrect input for the 'str' parameter in the 'strrep' MATLAB function, which must be a character array.
You can refer to the following example code snippet using ‘strrep’ MATLAB function:
% Correct input - Willn't thorw any error
data = {'123', 'hello', '456', 'world'};
strrep(data, 'o', 'a')
ans = 1x4 cell array
{'123'} {'hella'} {'456'} {'warld'}
% Wrong input - Will throw the same error
data = {123, 'hello', 456, 'world'};
strrep(data, 'o', 'a')
Error using strrep
Cell elements must be character vectors.
Refer to the following MATLAB Documentation to learn more about ‘strrep’ function: https://www.mathworks.com/help/releases/R2024b/matlab/ref/strrep.html
I hope this helps you!

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!