MATLAB Answers

0

Searching multiple strings at one time

Asked by Tyler Murray on 22 Sep 2016
Latest activity Commented on by Tyler Murray on 22 Sep 2016
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?

  0 Comments

Sign in to comment.

1 Answer

Answer by Walter Roberson
on 22 Sep 2016
 Accepted Answer

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 Comment

ah I have 16a, contains looks great! Wonder why it took them so long to implement that. Your solution works as well, thank you!

Sign in to comment.