MATLAB: Comparing datetime value from two tables

datetime

I have two tables a and b both containing time and price data. They are imported from csv files and therefore become two tables.
Both contain data of similar format but the datetime data in a is in minutes eg "11-07-17 09:00" and in b it's in dates eg "11-07-17".
I want to compare the price data given a date, something like "select * from b where b(:,1)=date(a(1,1))"
I'm extremely confused in trying to convert table into other format in order to compare them, I felt that there must be a very easy way to do it which i'm not aware of. someone pls help thanks so much!

Best Answer

A datetime always has an hour, minute, second component. The display format of the datetime array may be such that it is not shown, but it is always there. If you've read that array from a text file which didn't have a time component, then it defaults to 00:00:00.
You have several options to compare datetime arrays using just the date
  • You could set the time component in a to 00:00:00 and use standard comparison functions (e.g. ismember, or join)
  • You could extract the year, month and day from each with ymd and do array comparison (e.g. with ismember with the 'rows' option)
  • You could extract the date as strings (using char or datestr) and compare the strings.
Going with option 2, you could do:
ymda = zeros(height(a), 3);
[ymda(:, 1), ymda(:, 2), ymda(:, 3)] = ymd(a.Datetime);
ymdb = zeros(height(b), 3);
[ymdb(:, 1), ymdb(:, 2), ymdb(:, 3)] = ymd(b.Date);
queryresult = b(ismember(ymdb, ymda, 'rows'), :); %equivalent to select * from b where b.date = a.date