How to find the common dates between two different variables that are not the same size or format?
19 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have two different datetime arrays: dt which is (14782x1) and dt2 which is (47370x1). The formats between the two variables are also different where dt doesn't include minutes. For example, '9-Nov-2015 22:00:00'. While dt2 does include minutes so its like '9-Nov-2015 22:13:22'. I need to find the indexes where both variables have the same common time so I can plot other variables like wind speed. However, I'm having trouble getting the indexes. I've tried converting the two datetimes and using datenum and intersect but it doesn't really work. I've also tried ismember but depending on which variable I get first, I get two different sizes.
My code:
%get simultaenous data for ASCAT and HSRL
ff=datetime(dt2,'ConvertFrom','datenum'); %change format of ASCAT datenum so similar to HSRL format
%ff.Format = 'dd-MM-yyyy HH';
%ff=cellstr(ff);
%
ff=datetime(ff,'InputFormat','dd-MMM-yyyy HH:mm:ss','Format','dd-MMM-yyyy HH');
ASCAT_datenum2=datenum(ff);
%convert format of HSRL datetime
ff=datetime(dt,'ConvertFrom','datenum');
ff=datetime(ff,'InputFormat','dd-MMM-yyyy HH:mm:ss','Format','dd-MMM-yyyy HH');
HSRLdatenum2=datenum(ff);
HSRLdatenum2=HSRLdatenum2';
%find the same times
[val,pos]=intersect(ASCAT_datenum2,HSRLdatenum2);
[val2,pos2]=intersect(HSRLdatenum2,ASCAT_datenum2);
The length of val and val2 end up being 25x1, however I know that there's more common datetimes just by scrolling through the variables.
2 comentarios
Scott MacKenzie
el 25 de Oct. de 2021
Editada: Scott MacKenzie
el 25 de Oct. de 2021
I suspect using datenum is not the way to go. But first, a few things need to be clarified. If you want to plot other variables like wind speed, are these variable stored along with the datetime elements, for example, in a table?
By "same common time", are you referring to the same date and time, but ignoring the minutes and seconds?
Respuestas (2)
Walter Roberson
el 25 de Oct. de 2021
dtdt = datetime(dt, 'ConvertFrom', 'datenum', 'Format', 'dd-MMM-yyyy HH');
dtday = dateshift(dtdt, 'start', 'day');
%change format of ASCAT datenum so similar to HSRL format
dt2dt = datetime(dt2, 'ConvertFrom', 'datenum', 'Format', 'dd-MMM-yyyy HH');
dt2day = dateshift(dt2dt, 'start', 'day');
[common_dt, pos, pos2] = intersect(dtday, dt2day);
0 comentarios
Scott MacKenzie
el 25 de Oct. de 2021
Editada: Scott MacKenzie
el 25 de Oct. de 2021
% load dt and dt2, as per question
load test
% ignore minutes and seconds
dt = dateshift(dt,'start','hour');
dt2 = dateshift(dt2,'start','hour');
% find common datetime values in dt and dt2 (ignoring minutes and seconds)
[dt3, idx] = intersect(dt, dt2);
dt3 and idx are 25x1 vectors, so your initial code was correct, but there is no need to work with datenum vectors
0 comentarios
Ver también
Categorías
Más información sobre Calendar en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!