Finding common strings elements in array?

3 ビュー (過去 30 日間)
Ryan
Ryan 2013 年 9 月 30 日
コメント済み: Ryan 2013 年 10 月 2 日
Hi,
So this seems ridiculously easy but for some reason I can't find a solution. I imported an excel file into matlab and wanted to extract data after a specific date. The problem is that the elements are at the end in each date and not beginning.
I want to have an index of cells that have a common given year. They are formatted this way in each cell: '5/22/2000'
strmatch from my understanding finds common elements but mine are at the end....
  2 件のコメント
Image Analyst
Image Analyst 2013 年 9 月 30 日
Can you attach the workbook, or at least enough of it so we can try to read it in?
Ryan
Ryan 2013 年 10 月 2 日
Yes I will try to get that to you later today. Thanks!

サインインしてコメントする。

回答 (2 件)

Jan
Jan 2013 年 9 月 30 日
Do you want to search for the final part of a string? Then http://www.mathworks.com/matlabcentral/fileexchange/27032-strncmpr would help:
C = {'5/22/2000', '5/22/2001', '15/23/2000'};
index = find(strncmpr(C, '2000', 4));
% >> index = [1, 3]
  2 件のコメント
Ryan
Ryan 2013 年 10 月 1 日
Thanks for the response. When I run your function it says "Cannot find compiled Mex file".
Jan
Jan 2013 年 10 月 2 日
Then please read the instructions provided inside the file, especially the point "COMPILATION". There you find a link to download pre-compiled MEX files also.

サインインしてコメントする。


Jos (10584)
Jos (10584) 2013 年 10 月 2 日
編集済み: Jos (10584) 2013 年 10 月 2 日
Get the final part of the strings, and compare them:
C = {'5/22/2000', '5/22/2001', '15/23/2000'} % example from jan
tf = strcmp(cellfun(@(x) x(end-3:end),C,'un',0),'2000')
Another, easier approach:
[y,m,d] = datevec(C,'mm/dd/yyyy')
tf = y == 2000
but note that for this to work the dates should be valid. C{3} is NOT a valid date ( was that intentionally, Jan? ) so that is why tf(3) is false ...
  1 件のコメント
Ryan
Ryan 2013 年 10 月 2 日
Thanks for this Jos however I don't think the latter option is working. I imported an excel document that has a list of rows that are like below and it is giving me the error:
"[y, m, d] = datevec(txt(1:3,1),'mm/dd/yyyy'); Error using dtstr2dtvecmx Failed on converting date string to date number. Error in datevec (line 118) y = dtstr2dtvecmx(t,icu_dtformat);"
The array is like this:
'5/22/2000'
'CO'
'IL'
'5/30/2000'

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by