Filter löschen
Filter löschen

Synchronize two time series matlab R2015a

2 Ansichten (letzte 30 Tage)
tilfani oussama
tilfani oussama am 2 Mär. 2018
Beantwortet: tilfani oussama am 5 Mär. 2018
I have two time series SP and CAC40 returns, i would like to save only equal dates values, i have a matlab 2015a. Bests

Akzeptierte Antwort

Star Strider
Star Strider am 2 Mär. 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 Kommentar
tilfani oussama
tilfani oussama am 5 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

tilfani oussama
tilfani oussama am 5 Mär. 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!

Translated by