How to find the common dates between two different variables that are not the same size or format?
16 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Scott MacKenzie
2021 年 10 月 25 日
編集済み: Scott MacKenzie
2021 年 10 月 25 日
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?
回答 (2 件)
Walter Roberson
2021 年 10 月 25 日
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 件のコメント
Scott MacKenzie
2021 年 10 月 25 日
編集済み: Scott MacKenzie
2021 年 10 月 25 日
% 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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Calendar についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!