Determine which string in an array matched using contains()

13 visualizzazioni (ultimi 30 giorni)
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 Commento
Matt J
Matt J il 12 Gen 2023
Modificato: Matt J il 12 Gen 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.

Accedi per commentare.

Risposta accettata

Matt J
Matt J il 12 Gen 2023
Modificato: Matt J il 12 Gen 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 Commento
Mark Welker
Mark Welker il 12 Gen 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!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by