How can I make a logical array by comparing two date vectors?

조회 수: 6 (최근 30일)
Ekaterina Serikova
Ekaterina Serikova 2016년 4월 29일
편집: Stephen23 2025년 8월 27일
Hello, could you please help me on the following issue. I imported an Excel file containing the vector of dates (44 by 1, type - cell):
[temp, holidays]=xlsread('Holidays.xlsx', 'Tabelle1', 'A1:A44').
Then, I created the date vector with a step of 1 minute:
start_date = datenum('18-Apr-2015 16:30:00');
end_date = datenum('19-Apr-2015 10:00:00');
interval = 10/24/60;
date = datestr(start_date:interval:end_date);
My goal is to create a logical array that puts 1 if the date from the second (longer) vector equals the date in the first vector and 0 otherwise.
I tried the code below, but I have problems with data formats, which I cannot solve. Error using datenum (line 179) DATENUM failed.
Caused by: Error using dtstr2dtnummx Failed on converting date string to date number.
criteria=[];
for i=1:length(date)
if day(holidays(i))==day(date(i)) && month(holidays(i))==month(date(i)) && year(holidays(i))==year(date(i))
criteria(i)=1
else
criteria(i)=0
end
end
Can please anyone help on that? Thanks a lot in advance!
  댓글 수: 2
Azzi Abdelmalek
Azzi Abdelmalek 2016년 4월 29일
You can make your question clear by posting a sample of your imported data.
Ekaterina Serikova
Ekaterina Serikova 2016년 4월 29일
Ok, please, find attached. I want to compare the dates in the attached file with the dates generated by the code indicated in my question. Thank you in advance!

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2016년 4월 29일
편집: Stephen23 2016년 4월 29일
You could use ismember with the 'rows' option. It only takes two steps
  1. convert all of the dates into datevectors (not datenumbers) using datevec.
  2. call ismember on the first three columns of those dates (or pick whatever units you want to compare).
The output will be your desired logical vector.
Here is a full working example:
% all dates:
beg_date = datenum('18-Apr-2015 16:30:00');
end_date = datenum('19-Apr-2015 10:00:00');
interval = 10/24/60;
dat_mat = datevec(beg_date:interval:end_date);
% fake data:
holidays = {'18-Apr-2015 16:50:00';'19-Apr-2015 09:50:00'};
hol_mat = datevec(holidays)
% number of columns of the datevectors to compare:
N = 5;
idx = ismember(dat_mat(:,1:N),hol_mat(:,1:N),'rows');
and the output is a logical vector, exactly as requested. For this example it is true here:
>> find(idx)
ans =
3
105
This method has the advantage that you can very simply change the units that are being compared (by changing N), without needing to change any format strings.
  댓글 수: 2
Ekaterina Serikova
Ekaterina Serikova 2016년 4월 29일
편집: Ekaterina Serikova 2016년 4월 29일
Dear Stephen, I have a strange thing happening with the dates when I convert them from "cell" to "datevec" - not the same dates are in datevec, please find attached. What can be a problem? I use
hol=datevec(holidays);
Stephen23
Stephen23 2016년 4월 29일
편집: Stephen23 2025년 8월 27일
@Ekaterina Serikova: don't worry, this is not your fault! This is because lots of software uses the illogical mm/dd/yyyy as the default. Because most of the world generally uses sequences that progresses in order (dd/mm/yyyy or yyyy/mm/dd) you can imagine the confusion that this can cause!
The solution is simple: you should always specify the date format when converting the date:
vec = datevec(in_strings,'dd/mm/yyyy')
An even better solution is to always use an ISO 8601 date format for all date strings: these are always unambiguous and have the advantage that they sort simply into chronological order!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 4월 29일
You can use the ismember function
  댓글 수: 3
Azzi Abdelmalek
Azzi Abdelmalek 2016년 4월 29일
Ok, post a sample of your data
Azzi Abdelmalek
Azzi Abdelmalek 2016년 4월 29일
[~,a]=xlsread('holidays.xlsx');
b={'03-Apr-2015 16:30:00','06-Apr-2015 16:30:00','07-Apr-2015 16:30:00','08-Apr-2015 16:30:00'},;
bb=datestr(b,'dd.mm.yyyy'),
out=ismember(a,bb)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by