Reduce rows if dates don't match

I want to reduce the table MSCI by the rows which have a different date than the table AdjDate. Thus, I want to include only the dates whih are inluded in both tables As the dates are in different formats, I first converted the dates to numerical dates and tried to reduce them using the following code but it didnt give me the output i wanted to have
DateNum = datenum(AdjDate.TimeWindow)
DateNUMM = datenum(MSCI.Date)
DateTableM = array2table(DateNUMM)
AdjDateMSCI = [MSCI DateTableM];
AdjDateMSCI(ismember(AdjDateMSCI.Date,AdjDate.TimeWindow),:)=[];

4 Comments

dpb
dpb on 1 Jun 2019
Edited: dpb on 1 Jun 2019
  1. What are the two formats?
  2. Since a datenum is just a double, it's possible to have rounding issues using ismember in comparison of date numbers created by different means...
Use the newer datetime class which is more robust; a timetable object has some builtin synchronization tools as well...but first is "we need to see the data" to be able to say what specifically is the issue...
  1. The two formats are tables. The first one is a 1635 x 4 with format yy.MM.yyyy and the other is a 1664 x 7 table where the first column is in the format yyy-MM-dd. The date column which shall be compared is always the first column of the table.
  1. if I use datetitime it also doesnt produce the output i would like to have.
Thank you for your help!
If I use datetitime it also doesnt produce the output i would like to have
Most likely, you're doing something wrong. We can't tell you what if you don't give more details such as code and example data.
In particular, datetime does not care one bit about the actual formatting of the date to perform date comparison:
>> d1 = datetime('01.06.2019', 'Format', 'dd.MM.yyyy')
d1 =
datetime
01.06.2019
>> d2 = datetime('2019-06-01', 'Format', 'yyyy-MM-dd')
d2 =
datetime
2019-06-01
>> d1 == d2
ans =
logical
1
And as dpb said, if you have two tables for which you want to compute the intersection based on time, it can be done in just one line with synchronize if you convert the tables to timetables (not time series!).
Thanks, Guillaume -- I did mean the time table object NOT the time series! I have yet to find a use for it that it isn't more in the way than help...I'll fixup the note to match intent.

Sign in to comment.

Answers (0)

Categories

Asked:

AU
on 1 Jun 2019

Edited:

dpb
on 1 Jun 2019

Community Treasure Hunt

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

Start Hunting!