Synchronize two time series matlab R2015a

3 views (last 30 days)
I have two time series SP and CAC40 returns, i would like to save only equal dates values, i have a matlab 2015a. Bests

Accepted Answer

Star Strider
Star Strider on 2 Mar 2018
Try this:
[D1,S1,R1] = xlsread('SP dated.xls');
[D2,S2,R2] = xlsread('CAC40 dated.xls');
S1ds = R1(2:end,:); % Valid ‘SP’ Data (Omitting Header)
S2ds = R2(2:end,:); % Valid ‘CAC40’ Data (Omitting Header)
dn1 = datenum(S1ds(:,1), 'yyyy-mm-dd'); % Convert To ‘Date Numbers’
dn2 = datenum(S2ds(:,1), 'yyyy-mm-dd'); % Convert To ‘Date Numbers’
[Dc, ia, ic] = intersect(dn1, dn2, 'stable'); % Common Date Numbers
SP = S1ds(ia,:); % ‘SP’ Result Matrix
CAC40 = S2ds(ic,:); % ‘CAC40’ Result Matrix
SP_First10 = SP(1:10,:); % Examine Output (Delete Later)
CAC40_First10 = CAC40(1:10,:); % Examine Output (Delete Later)
The code is straightforward, and the comments document what each line does. The 'stable' argument to the intersect call keeps everything in the original order rather than sorting it.
The sizes of the output arrays ‘SP’ and ‘CAC40’ are the same sizes (as they should be), and both less than the original of either of the original arrays, indicating that they now have only common dates. I looked at a range of them but not all, and the result appears to be what you want.
I included ‘SP_First10’ and ‘CAC40_First10’ to let you easily examine that range or any other range you want, to verify that the data are correct.
  1 Comment
tilfani oussama
tilfani oussama on 5 Mar 2018
Thank you for your code, i get this message
Error using datenum (line 178)
DATENUM failed.
Caused by: Error using dtstr2dtnummx
Failed on converting date string to date number.

Sign in to comment.

More Answers (1)

tilfani oussama
tilfani oussama on 5 Mar 2018
That works with
[D1,S1,R1] = xlsread('SP dated.xls');
[D2,S2,R2] = xlsread('CAC40 dated.xls');
S1ds = R1(2:end,:);
S2ds = R2(2:end,:);
dn1 = datenum(S1ds(:,1), 'dd/mm/yyyy');
dn2 = datenum(S2ds(:,1), 'dd/mm/yyyy');
[Dc, ia, ic] = intersect(dn1, dn2, 'stable');
SP = S1ds(ia,:); % ‘SP’ Result Matrix
CAC40 = S2ds(ic,:);

Community Treasure Hunt

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

Start Hunting!