Determine which string in an array matched using contains()
12 次查看(过去 30 天)
显示 更早的评论
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
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;
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!