Simple wild card function for ismember??

조회 수: 22 (최근 30일)
bugguts99
bugguts99 2011년 7월 19일
I have a list of dates and times as string values and (e.g '29/1/2010 20:30:00' etc) and I would like to find all values at a specific time, regardless of day.
Is there a simple wild card function that I can use with ismember to identify these? I have tried:
idx = ismember(Timestamp_str, {'*20:30:00'});
I have also tried regexp, searched the web and help files to no avail.....

채택된 답변

Walter Roberson
Walter Roberson 2011년 7월 19일
No, there is no wildcard with ismember.
If your Timestamp_str is an array of characters all the same length, then
strcmp(cellstr(Timestamp_str(:,end-7:end)),'20:30:00')
If your Timestamp_str is a cell array of characters, then
cellfun(@(S) strcmp(S(end-7:end),'20:30:00'), Timestamp_str)
Alternately with regexp,
~cellfun(@isempty,regexp(Timestamp_str, '20:30:00$'))
If you just want the true/false of whether there was a match or not. The regexp itself will return the index of the trailing 23:30:00 in each string, with an empty cell for cell entries that do not match.

추가 답변 (2개)

Andrei Bobrov
Andrei Bobrov 2011년 7월 19일
d1 = datenum(Timestamp_str);
d2 = datenum('00/0/0000 20:30:00');
out = Timestamp_str(abs(rem(d1,1)-d2)<1e6*eps,:)
ADD variant with 'datevec'
dv = datevec(Timestamp_str);
out = Timestamp_str(all(bsxfun(@eq,dv(:,4:6),[20 30 0]),2),:);
adding in answer on the comment Walter's about milliseconds
dv(:,end) = round(dv(:,end));
dv = datevec(datenum(dv));
  댓글 수: 3
Andrei Bobrov
Andrei Bobrov 2011년 7월 19일
Dear Walter! I completely agree with you and thinking about using 'datevec'.
Add variant with 'datevec'
Walter Roberson
Walter Roberson 2011년 7월 19일
Though 20:29:59.5 and up to but excluding 20:30:00.5 would probably print out as 20:30:00, so rounding the seconds + milliseconds combination might be called for.

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


Jan
Jan 2011년 7월 19일
A fast method is coparing the strings from the right: FEX: strcmpr.
idx = strncmpr(Timestamp_str, '20:30:00', 8);

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by