Determine which string in an array matched using contains()
13 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Mark Welker
am 12 Jan. 2023
Kommentiert: Mark Welker
am 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 Kommentar
Akzeptierte Antwort
Matt J
am 12 Jan. 2023
Bearbeitet: Matt J
am 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 Kommentar
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!