user32882
user32882

Reputation: 5907

Error with strfind on matlab

Can anyone make sense of this? This is driving me crazy. Why am I getting the error?

>> Fullnx{i,2}

ans = 

    '01ST'

>> street(j,1)

ans = 

    ' AVE'

>> strfind('01ST', ' AVE')

ans =

     []

>> strfind(Fullnx{i,2},street(j,1))
Error using cell/strfind (line 32)
If any of the input arguments are cell arrays, the first must be a
cell array of strings and the second must be a character array.

Upvotes: 1

Views: 2344

Answers (1)

rayryeng
rayryeng

Reputation: 104555

That's because street(j,1) is a cell array itself. You are slicing into the cell array, but not unpacking its contents. As such, both inputs into strfind are cell arrays, and the error says that one input must be a cell array and the other a character array if this happens.

You probably want to compare two character arrays instead, and so do this:

>> strfind(Fullnx{i,2}, street{j,1});

Double check this yourself by examining the class of each input. Do class(Fullnx{i,2}) and class(street(j,1)) and examine the data types yourself. You'll see that one is of type char and the other is of type cell. However, if you really want to get this to work with your syntax, simply swap the inputs around:

>> strfind(street(j,1), Fullnx{i,2});

The usefulness of the cell array input is that if you have a bunch of strings in a cell array, the second input is asking for what pattern / text you are trying to search for in each cell in the cell array (first input). The output will give you a cell array of numeric arrays that give you the indices of where each occurrence of the pattern / text you are trying to find.

As a bonus, here's a sample run through:

>> c = {'hello', 'how', 'are', 'you'};
>> out = strfind(c, 'el')

out = 

    [2]    []    []    []

I have a cell array of strings in c, and I want to find the characters 'el' over each string in the cell array. The output is another cell array where we can't find this sequence of characters other than the first string, where it starts at index 2.

Upvotes: 4

Related Questions