Strmatch with multiple inputs possible?

9 views (last 30 days)
Tessa Aus
Tessa Aus on 16 Jun 2016
Edited: per isakson on 18 Jun 2016
Was wondering if Strmatch can have multiple inputs checking the outputs of the users array? For example I have an array (5,1) where each row has a different name such as Cat, Dog, Alligator... I would like to see how many times each have been repeated in a (:,1) array. Strmatch is compatable as long as my array is only a 1:1 size but any other size it outputs a zero as amount of times a word has been repeated. Maybe there is something better then strmatch? My code is below.. Where str is the (current) (1:1) and want the (5:1) and MeterType is the array I am checking the strings to.
ind = strmatch(str, MeterType);
numTimesEntered = length(ind);
save numTimesEntered;

Accepted Answer

dpb
dpb on 16 Jun 2016
Edited: dpb on 17 Jun 2016
Not directly that I can think of, no...can get moderately close with cellfun although it needs that the target array be static. Try
nCounts=cellfun(@(s) length(strmatch(s,arry)), str,'uniform',0);
where
str is your list of "to look for's" and
arry is the target array
ADDENDUM
BTW, you'll likely want to wrap the above inside cell2mat to get a double instead of cell array which cellfun will return...
nCounts=cell2mat(cellfun(@(s) length(strmatch(s,arry)), str,'uniform',0));
  4 Comments
Tessa Aus
Tessa Aus on 16 Jun 2016
Edited: dpb on 17 Jun 2016
seems like it is not working here is the error, the only thing I changed was Arry to the target arrays name. str stayed the same because it is the array the inputs are getting stored into that will be compared against the other array.
Error using cellfun
Input #2 expected to be a cell array, was char instead.
Error in UserInit2>Save_Callback (line 148)
nCounts=cellfun(@(s) length(strmatch(s,MeterType)), str,'uniform',0);
end
dpb
dpb on 16 Jun 2016
Edited: dpb on 16 Jun 2016
It's what it says it is, you passed an array of character strings, not a cellstr array. cellfun only works with cells, arrayfun won't work here because it operates on each element of the array or for a character array that would be over each character in each string, not the entire string.
While cell arrays have some drawbacks for numeric content, for strings they're often much handier. Your original code wouldn't work for multiple strings in a character array, either, so you've not lost anything by making the switch.
Try passing cellstr(str) and joy should ensue for the immediate problem but in the bigger picture you'll want to recast into using cellstr overall in creating the arrays in which case won't be needing to cast individually.
PS: If this doesn't make sense to you, then you need to read the sections in the doc's "Getting Started" on character vis-a-vis cell strings but for a quick primer of what's the issue, if you write
s='Cat';
then
>> whos s
Name Size Bytes Class Attributes
s 1x3 6 char
>>
Note that the size isn't one something called a string but an array of three class char elements. It's no different other than class of writing
v=[1 3 4];
>> whos v
Name Size Bytes Class Attributes
v 1x3 24 double
>>
Identical except for the Class and the size of the element in memory (8 bytes/double instead of 2 bytes/char).
Hence, what you get when you write
>> s
s =
Cat
>> s(1)
ans =
C
>>
that just like with a numeric array, if you address the first element of the char array you only get one element thereof, not the whole array which you get if you omit any indexing expression; that's the shortcut for "the whole thing".
OTOH, if you write
c=cellstr('Cat'); % or, equivalently
c={'Cat'}; % or, having already defined s
c=cellstr(s); % then
>> whos c
Name Size Bytes Class Attributes
c 1x1 66 cell
>>
Now you do have one something; a cell which holds the char array 'Cat'. There's an obvious memory overhead to pay for this as it takes 66 bytes instead of just 6, but it's a small price to pay when you can then write
>> c={'Cat'; 'Dog'; 'Guppy'}
c =
'Cat'
'Dog'
'Guppy'
>> c(2)
ans =
'Dog'
>>
and get the whole thing. Plus, it's the only way you can do the type of processing on the elements of the array as you asked originally and get the full string for each element instead of a single character.
To finish the tutorial, consider
>> arrayfun(@disp,s)
C
a
t
>> cellfun(@disp,c)
Cat
Dog
Guppy
>> cellfun(@disp,s)
Error using cellfun
Input #2 expected to be a cell array, was char instead.
>>
>> cellfun(@disp,cellstr(s)) % the temporary fix noted earlier...
Cat
>>
The last should look familiar? :)

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!