I am trying to find the indexes in a struct 'data.orig_reg' where the corresponding strings I am trying to find is 'a_13'. The following code works just fine.
CVT_PAT = find(~cellfun('isempty',strfind({data.orig_reg},'a_13')))';
[sharedCVTVals,~] = intersect(CVT,CVT_PAT);
If I implement multiple string searches such as:
TEST = {'a_13','a_14','a_164'}.
CVT_PAT = find(~cellfun('isempty',strfind({data.orig_reg},TEST)))';
[sharedCVTVals,~] = intersect(CVT,CVT_PAT);
This returns an error:
Error using strfind
PATTERN must be a string scalar or character vector.
This is due to strfind only takes in one string at the time as faar as I can tell... Is there a way to make this code work by using multiple strings as an input like the variable TEST described above?

 채택된 답변

Stephen23
Stephen23 2017년 4월 20일
편집: Stephen23 2017년 4월 20일

3 개 추천

One solution: wrap it all up in another cellfun:
>> C = {'A','B','xxxa_13x','xxa_14x','C','a_13'};
>> TEST = {'a_13','a_14','a_164'};
>> out = cellfun(@(s)find(~cellfun('isempty',strfind(C,s))),TEST,'uni',0);
>> out{:}
ans =
3 6
ans = 4
ans = []
Note that you did not specify how you want the output: do you want to be able to distinguish which string was matched, or which pattern was matched, or both, or neither (e.g. a simple count of how many matches were made). Each of these could be coded differently.

댓글 수: 8

Thank you very much! This is exactly what I was looking for:D
Daniel Eidsvåg
Daniel Eidsvåg 2017년 4월 20일
편집: Daniel Eidsvåg 2017년 4월 20일
I see I didn't answer your question, I want the output to give me the index of the string i want to compare (the exact string). Is there a way to that? Because some of the strings I wanted to compare added similar strings f.example 'a_20' also added 'a_21'.
Stephen23
Stephen23 2017년 4월 21일
편집: Stephen23 2017년 4월 21일
@Daniel Eidsvåg: I don't understand your comment. To clarify: you want to get the index of strings in C that match any of the patterns in TEST.
For example, for my demo code, the desired output would be [3,4,6], because strings in positions 3,4,6 match some patterns (but strings 1,2,5 do not).
@Stephen Cobeldick: To give another small example from my data.
>> C = {'a_2','a_3','a_4','a_14','a_20','a_21', 'a_22'};
>> TEST = {'a_2','a_20','a_164'};
>> out = cellfun(@(s)find(~cellfun('isempty',strfind(C,s))),TEST,'uni',0);
From out I get a 1x3 cell with the following result: [1, 5, 6, 7], [5], [].
My problem is that a_2 should only extract 'a_2' and not 'a_20','a_21', 'a_22'.
Does this elaborate my question better?
I need in this scenario a 1x3 cell which gives me the following result: [1], [5], []
Stephen23
Stephen23 2017년 4월 21일
@Daniel Eidsvåg: okay, that is clearer. Thank you for the example output.
I have one more question: can the patterns (in TEST) match part of the strings, or should they match the complete string? For example, do you need to detect the pattern 'Y' in the string 'XYZ'? Or are the patterns always equal to the complete string, e.g. match pattern 'XYZ' to string 'XYZ', but do not match pattern 'Y' (because it does not match the whole string).
Daniel Eidsvåg
Daniel Eidsvåg 2017년 4월 21일
편집: Daniel Eidsvåg 2017년 4월 21일
@Stephen Cobelldick: The patterns (in TEST) they should match the complete string. My patterns are such as described in 'C' and 'TEST', e.g. they need to match the whole string
Stephen23
Stephen23 2017년 4월 21일
편집: Stephen23 2017년 4월 21일
Then we should use strcmp instead of strfind:
>> C = {'a_2','a_3','a_4','a_14','a_20','a_21', 'a_22'};
>> TEST = {'a_2','a_20','a_164'};
>> cellfun(@(s)find(strcmp(s,C)),TEST,'uni',0)
ans =
[1]
[5]
[]
Thank you very much! It now works as intended =)

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

추가 답변 (1개)

Tao Wang
Tao Wang 2022년 1월 13일

0 개 추천

mystr={'123a1','ab','111'};
pat = ("a"|"b");
a = strfind(mystr,pat)
% can get index of each match
ab = contains(mystr,pat)
% logical result

카테고리

도움말 센터File Exchange에서 Cell Arrays에 대해 자세히 알아보기

질문:

2017년 4월 20일

답변:

2022년 1월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by