Determine which string in an array matched using contains()

조회 수: 15 (최근 30일)
Mark Welker
Mark Welker 2023년 1월 12일
댓글: Mark Welker 2023년 1월 12일
I am working with contains and have a string array that I am using to determine whether another string array contains any of those values.
Right now I am using an if statement to determine whether or not they do and it works just fine.
The issue I am having is I would like to know which specific string matched in the supplied pattern array.
Example:
if contains("AUTOZONE #0093",["Amazon","Autozone","Home Depot"],'IgnoreCase',true)
% standardize name
end
So this reads as true, which is good but what I am trying to do is now take that test string and convert it to match the pattern found. So it would ideally be like:
stringArray = ["",,,,,,,,,,,,"AUTOZONE #0093"]; % you get the point its a stringArray
vendorArray = ["Amazon","Autozone","Home Depot"]; % pattern string Array
if contains(stringArray(48,1),vendorArray,'IgnoreCase',true)
stringArray(48,1) = vendorArray(1,2);
%the issue here being I need to get the index value of the matched pattern i.e. 2
end
I can't flip the pattern and the search, I've tried that. I've tried strcmp, matches, ismember, find(strcmp). Can't get it to output what I need...
Any help would be appreciated I'm just not sure if there already exists a way to pull the index or if I need to restructure my test case here.
Thanks!
  댓글 수: 1
Matt J
Matt J 2023년 1월 12일
편집: Matt J 2023년 1월 12일
The issue I am having is I would like to know which specific string matched in the supplied pattern array.
You imply that at most one pattern will be matched.

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

채택된 답변

Matt J
Matt J 2023년 1월 12일
편집: Matt J 2023년 1월 12일
Easiest thing would be to just loop over the shorter of the two arrays, probably the pattern array:
stringArray=stringArray(:);
vendorArray=vendorArray(:)'; n=numel(vendorArray);
clear T
for 1=n:-1:1
T(:,i)=contains(stringArray,vendorArray(i),'IgnoreCase',1);
end
locations=T*(1:n).';
locations(sum(T,2)~=1)=nan;
  댓글 수: 1
Mark Welker
Mark Welker 2023년 1월 12일
This works, and allows me to match the array pattern. You might want to make this update:
for i=n:-1:1
T(:,i)contains(stringArray,vendorArray(i),'IgnoreCase',true);
end
to avoid any confusion in the future.
Thanks for the help!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by