Determine which string in an array matched using contains()

13 vues (au cours des 30 derniers jours)
Mark Welker
Mark Welker le 12 Jan 2023
Commenté : Mark Welker le 12 Jan 2023
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 commentaire
Matt J
Matt J le 12 Jan 2023
Modifié(e) : Matt J le 12 Jan 2023
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.

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 12 Jan 2023
Modifié(e) : Matt J le 12 Jan 2023
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 commentaire
Mark Welker
Mark Welker le 12 Jan 2023
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!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by