Searching multiple strings at one time

조회 수: 75 (최근 30일)
Tyler Murray
Tyler Murray 2016년 9월 22일
댓글: Tao Wang 2022년 1월 13일
I am importing a text file and want to eliminate data I don't need to further study. The array is a 40 x 1 and each row contains its own sentence with data. I want to search for multiple strings since I know what the interesting data points start with. I don't want to use strfind multiple times and combine at the end because that will just make the code unnecessarily long and slow. Ideally, I'd have a scenario such as
exampleStrings = {'This test was run hot'; 'This test failed', 'This test failed', 'This test ran at hot and cold', 'This test ran at ambient'}
check = {'hot', 'cold', 'med'}
x = strfind(exampleStrings, check)
x = [1; 0; 0; 1; 0] (output)
Essentially, the string search cannot search for a perfect match or else it will return all 0's which is why I have been struggling. Any ideas?
  댓글 수: 1
Tao Wang
Tao Wang 2022년 1월 13일
mystr={'123a1','ab','111'};
pat = ("a"|"b");
a = strfind(mystr,pat)
% can get index of each match
ab = contains(mystr,pat)
% logical result

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

채택된 답변

Walter Roberson
Walter Roberson 2016년 9월 22일
If you have R2016b then this would be a good place to use the new contains() string operation.
Otherwise:
~cellfun(@isempty,regexp(exampleStrings,strjoin(check,'|')))
  댓글 수: 1
Tyler Murray
Tyler Murray 2016년 9월 22일
ah I have 16a, contains looks great! Wonder why it took them so long to implement that. Your solution works as well, thank you!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by