Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Finding the position of a specific string in a cell array of strings

Subject: Finding the position of a specific string in a cell array of strings

From: Samuel Chapman

Date: 29 Oct, 2010 19:57:03

Message: 1 of 8

I have been trying to figure this out today and it is really bugging me:

I have a cell array of strings, let's call is A, like this:

A =

'abc'
'123'
'def'
'456'

I want to give a query and find out which cell, if any, has the query string. For example, if I want to find out which cell 'def' is in, I want it to tell me that it is in the third cell.

If I use regexp to do this, it gives me an answer like this:

regexp(A, 'def')

[]
[]
[1]
[]

Which is not very helpful to me, because I just want a number back--I don't want to have to search for that 1 to find out which cell position in the array it's in (in the program I am working on, I will have several thousand such cells to search through).

I can't figure out how I might just get that number. Is there a way to do that in Matlab? I thought that maybe converting the cell array,using the char function, would work, but I can't figure out what to do with that, either.

Any help would be greatly appreciated!

Subject: Finding the position of a specific string in a cell array of strings

From: Jan Simon

Date: 29 Oct, 2010 20:17:05

Message: 2 of 8

Dear Samuel,

if you are not satisfied by REGEXP, read the help text:
  help regexp
At te end you find the "See also" line, which means something like: "Users who were interested in this function, were interested in these functions also:".
There you find STRCMP and STRMATCH. While the later is slow,
http://www.mathworks.com/matlabcentral/newsreader/view_thread/294626
FIND(STRCMP) solves your problem.

Good luck, Jan

Subject: Finding the position of a specific string in a cell array of strings

From: Sean

Date: 29 Oct, 2010 20:18:03

Message: 3 of 8

"Samuel Chapman" <samdchap@indiana.edu> wrote in message <iaf8uf$f0b$1@fred.mathworks.com>...
> I have been trying to figure this out today and it is really bugging me:
>
> I have a cell array of strings, let's call is A, like this:
>
> A =
>
> 'abc'
> '123'
> 'def'
> '456'
>
> I want to give a query and find out which cell, if any, has the query string. For example, if I want to find out which cell 'def' is in, I want it to tell me that it is in the third cell.
>
> If I use regexp to do this, it gives me an answer like this:
>
> regexp(A, 'def')
>
> []
> []
> [1]
> []
>
> Which is not very helpful to me, because I just want a number back--I don't want to have to search for that 1 to find out which cell position in the array it's in (in the program I am working on, I will have several thousand such cells to search through).
>
> I can't figure out how I might just get that number. Is there a way to do that in Matlab? I thought that maybe converting the cell array,using the char function, would work, but I can't figure out what to do with that, either.
>
> Any help would be greatly appreciated!

One way (just for fun):
B1 = nonzeros(cell2mat(cellfun(@(x,y)strfind(x,'def').*y,A,num2cell(1:length(A)).','uni',false)));

Why not just do a loop?

B2 = zeros(size(A));
for ii = 1:length(B2)
    if strfind(A{ii},'def')
        B2(ii)=ii;
    end
end
B2 = B2(B2~=0);

It'll probably be fasted

Subject: Finding the position of a specific string in a cell array of strings

From: james bejon

Date: 29 Oct, 2010 20:19:03

Message: 4 of 8

strcmp('123', A)

or

find(strcmp('123', A))

Subject: Finding the position of a specific string in a cell array of strings

From: Samuel Chapman

Date: 29 Oct, 2010 20:23:04

Message: 5 of 8

Here is some information about what I am ultimately trying to do:

I have a list of ordered pairs, each with a numerical value associated with them:

A X n1
A Y n2
A Z n3
B X n4
B Y n5
B Z n6
C X n7
C Y n8
C Z n9
. . .
. . .



I want to create a matrix of these values like this:

    A B C . . .
X n1 n4 n7
Y n2 n5 n8
Z n3 n6 n9
.
.
.


One big problem with this is that the categories denoted by the letters are names, not numbers.

Subject: Finding the position of a specific string in a cell array of strings

From: Samuel Chapman

Date: 29 Oct, 2010 20:28:04

Message: 6 of 8

Jan, James, strcmp is what I needed. Thank you very much!

I have used strcmp before, but it has been a while...

Sean, about the loop, I did not want to do that because I think it would probably take too long.

Have a great day, guys!

Subject: Finding the position of a specific string in a cell array of strings

From: Sean

Date: 29 Oct, 2010 20:36:03

Message: 7 of 8

"Samuel Chapman" <samdchap@indiana.edu> wrote in message <iafaf8$opo$1@fred.mathworks.com>...
> Here is some information about what I am ultimately trying to do:
>
> I have a list of ordered pairs, each with a numerical value associated with them:
>
> A X n1
> A Y n2
> A Z n3
> B X n4
> B Y n5
> B Z n6
> C X n7
> C Y n8
> C Z n9
> . . .
> . . .
>
>
>
> I want to create a matrix of these values like this:
>
> A B C . . .
> X n1 n4 n7
> Y n2 n5 n8
> Z n3 n6 n9
> .
> .
> .
>
>
> One big problem with this is that the categories denoted by the letters are names, not numbers.

Such as this?:
%%%
Vals = {'A' 'X' 'n1'
'A' 'Y' 'n2'
'A' 'Z' 'n3'
'B' 'X' 'n4'
'B' 'Y' 'n5'
'B' 'Z' 'n6'
'C' 'X' 'n7'
'C' 'Y' 'n8'
'C' 'Z' 'n9'}

top = unique(Vals(:,1)).';
side = unique(Vals(:,2)).';
middle = reshape(Vals(:,3),length(side),length(top));

final = cell(length(side)+1,length(top)+1);
final(2:end,1) = side;
final(1,2:end) = top;
final(2:end,2:end) = middle;
%%%

Subject: Finding the position of a specific string in a cell array of strings

From: james bejon

Date: 29 Oct, 2010 20:38:03

Message: 8 of 8

A = {'A' 'X' 3; 'A' 'Y' 4.5; 'A' 'Z' 0.1; 'B' 'X' 10; 'B' 'Y' 20; 'B' 'Z' 0.1};
[Junk, Junk, Subs] = arrayfun(@(x) unique(A(:, x)), 2:-1:1, 'UniformOutput', 0);
accumarray(Subs, cell2mat(A(:, 3)))

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us