Thread Subject: How to match string for cell array strmatch() in Matlab

Subject: How to match string for cell array strmatch() in Matlab

From: Xiaoxiao

Date: 3 Dec, 2008 20:28:32

Message: 1 of 6

Hi:

I have a string cell array like

v={{'aaaa'}, {'bb'}, {'ccc'}, {'dd'}}

v =

    {1x1 cell} {1x1 cell} {1x1 cell} {1x1 cell}

and I would like to use strmatch() to located the cell array index in
which the string contains 'a' for example. I used below command:

 idx = strmatch('a', v)
??? Error using ==> cell.strmatch at 18
Requires character array or cell array of strings as inputs.

K>> idx = strmatch('a', v(:))
??? Error using ==> cell.strmatch at 18
Requires character array or cell array of strings as inputs.

 idx = strmatch('a', v{:})
??? Error using ==> cell.strmatch
Too many input arguments.

I wrote v like above rather than v = {'aaa', 'bbb', 'cc', 'ddd'}
directly is because I got something like the original v above format
when I used textscan() function to read each line from a textfile and
then I need to parse the textscan results using the strmatch but met
same errors as I got above.

Any help wil be appreciated.

Subject: How to match string for cell array strmatch() in Matlab

From: Stuart McGarrity

Date: 3 Dec, 2008 21:00:21

Message: 2 of 6

Xiaoxiao <xiaoxiaoyang@live.com> wrote in message <e4c5e8e5-865e-44b8-b8a2-b2f19f8edb83@20g2000yqt.googlegroups.com>...
> Hi:
>
> I have a string cell array like
>
> v={{'aaaa'}, {'bb'}, {'ccc'}, {'dd'}}
>
> v =
>
> {1x1 cell} {1x1 cell} {1x1 cell} {1x1 cell}
>
> and I would like to use strmatch() to located the cell array index in
> which the string contains 'a' for example. I used below command:
>
> idx = strmatch('a', v)
> ??? Error using ==> cell.strmatch at 18
> Requires character array or cell array of strings as inputs.
>
> K>> idx = strmatch('a', v(:))
> ??? Error using ==> cell.strmatch at 18
> Requires character array or cell array of strings as inputs.
>
> idx = strmatch('a', v{:})
> ??? Error using ==> cell.strmatch
> Too many input arguments.
>
> I wrote v like above rather than v = {'aaa', 'bbb', 'cc', 'ddd'}
> directly is because I got something like the original v above format
> when I used textscan() function to read each line from a textfile and
> then I need to parse the textscan results using the strmatch but met
> same errors as I got above.
>
> Any help wil be appreciated.

You will probabaly need to use cellfun or a loop as you have the extra cell level. If you just want to know if there is an 'a' but don't care where in the string it is, you could use:
>> cellfun(@(x) ~isempty(strfind(x{1},'a')),v)

ans =

     1 0 0 0

Subject: How to match string for cell array strmatch() in Matlab

From: Xiaoxiao

Date: 3 Dec, 2008 21:17:11

Message: 3 of 6

On Dec 3, 4:00=A0pm, "Stuart McGarrity"
<stuart.mcgarrity.nos...@mathworks.com> wrote:
> Xiaoxiao <xiaoxiaoy...@live.com> wrote in message <e4c5e8e5-865e-44b8-b8a=
2-b2f19f8ed...@20g2000yqt.googlegroups.com>...
> > Hi:
>
> > I have a string cell array like
>
> > v=3D{{'aaaa'}, {'bb'}, {'ccc'}, {'dd'}}
>
> > v =3D
>
> > =A0 =A0 {1x1 cell} =A0 =A0{1x1 cell} =A0 =A0{1x1 cell} =A0 =A0{1x1 cell=
}
>
> > and I would like to use strmatch() to located the cell array index in
> > which the string contains 'a' for example. I used below command:
>
> > =A0idx =3D strmatch('a', v)
> > ??? Error using =3D=3D> cell.strmatch at 18
> > Requires character array or cell array of strings as inputs.
>
> > K>> idx =3D strmatch('a', v(:))
> > ??? Error using =3D=3D> cell.strmatch at 18
> > Requires character array or cell array of strings as inputs.
>
> > =A0idx =3D strmatch('a', v{:})
> > ??? Error using =3D=3D> cell.strmatch
> > Too many input arguments.
>
> > I wrote v like above rather than v =3D {'aaa', 'bbb', 'cc', 'ddd'}
> > directly is because I got something like the original v above format
> > when I used textscan() function to read each line from a textfile and
> > then I need to parse the textscan results using the strmatch but met
> > same errors as I got above.
>
> > Any help wil be appreciated.
>
> You will probabaly need to use cellfun or a loop as you have the extra ce=
ll level. If you just want to know if there is an 'a' but don't care where =
in the string it is, you could use:
>
> >> cellfun(@(x) ~isempty(strfind(x{1},'a')),v)
>
> ans =3D
>
> =A0 =A0 =A01 =A0 =A0 0 =A0 =A0 0 =A0 =A0 0- Hide quoted text -
>
> - Show quoted text -

Thank you very much for the help, Stuart. It is very helpful. Here I
have one more question. Can you tell me why you need to use x{1}
instead of x directly in the strfind() function? Thanks again.

Subject: How to match string for cell array strmatch() in Matlab

From: Stuart McGarrity

Date: 3 Dec, 2008 21:53:02

Message: 4 of 6

Xiaoxiao <xiaoxiaoyang@live.com> wrote in message <bfe4a0fb-0fcd-4df2-bb9b-678743c2e104@j39g2000yqn.googlegroups.com>...
> On Dec 3, 4:00=A0pm, "Stuart McGarrity"
> <stuart.mcgarrity.nos...@mathworks.com> wrote:
> > Xiaoxiao <xiaoxiaoy...@live.com> wrote in message <e4c5e8e5-865e-44b8-b8a=
> 2-b2f19f8ed...@20g2000yqt.googlegroups.com>...
> > > Hi:
> >
> > > I have a string cell array like
> >
> > > v=3D{{'aaaa'}, {'bb'}, {'ccc'}, {'dd'}}
> >
> > > v =3D
> >
> > > =A0 =A0 {1x1 cell} =A0 =A0{1x1 cell} =A0 =A0{1x1 cell} =A0 =A0{1x1 cell=
> }
> >
> > > and I would like to use strmatch() to located the cell array index in
> > > which the string contains 'a' for example. I used below command:
> >
> > > =A0idx =3D strmatch('a', v)
> > > ??? Error using =3D=3D> cell.strmatch at 18
> > > Requires character array or cell array of strings as inputs.
> >
> > > K>> idx =3D strmatch('a', v(:))
> > > ??? Error using =3D=3D> cell.strmatch at 18
> > > Requires character array or cell array of strings as inputs.
> >
> > > =A0idx =3D strmatch('a', v{:})
> > > ??? Error using =3D=3D> cell.strmatch
> > > Too many input arguments.
> >
> > > I wrote v like above rather than v =3D {'aaa', 'bbb', 'cc', 'ddd'}
> > > directly is because I got something like the original v above format
> > > when I used textscan() function to read each line from a textfile and
> > > then I need to parse the textscan results using the strmatch but met
> > > same errors as I got above.
> >
> > > Any help wil be appreciated.
> >
> > You will probabaly need to use cellfun or a loop as you have the extra ce=
> ll level. If you just want to know if there is an 'a' but don't care where =
> in the string it is, you could use:
> >
> > >> cellfun(@(x) ~isempty(strfind(x{1},'a')),v)
> >
> > ans =3D
> >
> > =A0 =A0 =A01 =A0 =A0 0 =A0 =A0 0 =A0 =A0 0- Hide quoted text -
> >
> > - Show quoted text -
>
> Thank you very much for the help, Stuart. It is very helpful. Here I
> have one more question. Can you tell me why you need to use x{1}
> instead of x directly in the strfind() function? Thanks again.

In the statement: cellfun(@(x) ~isempty(strfind(x{1},'a')),v)

...cellfun takes each element of the cell array v and passes it to your anonymous function, in this case @(x) ~isempty(strfind(x{1},'a')).

Now, as your cell array array is in fact an array of cells, in the anonyous function x is a cell. e.g.

>> x=v{1};
>> whos
  Name Size Bytes Class Attributes
  v 1x4 502 cell
  x 1x1 68 cell

You need to index into the cell 'x' (using {} braces) to reach the string.

>> c=x{1};
>> whos
  Name Size Bytes Class Attributes
  c 1x4 8 char
  v 1x4 502 cell
  x 1x1 68 cell

Stuart

Subject: How to match string for cell array strmatch() in Matlab

From: Xiaoxiao

Date: 3 Dec, 2008 22:17:17

Message: 5 of 6

On Dec 3, 4:53=A0pm, "Stuart McGarrity"
<stuart.mcgarrity.nos...@mathworks.com> wrote:
> Xiaoxiao <xiaoxiaoy...@live.com> wrote in message <bfe4a0fb-0fcd-4df2-bb9=
b-678743c2e...@j39g2000yqn.googlegroups.com>...
> > On Dec 3, 4:00=3DA0pm, "Stuart McGarrity"
> > <stuart.mcgarrity.nos...@mathworks.com> wrote:
> > > Xiaoxiao <xiaoxiaoy...@live.com> wrote in message <e4c5e8e5-865e-44b8=
-b8a=3D
> > 2-b2f19f8ed...@20g2000yqt.googlegroups.com>...
> > > > Hi:
>
> > > > I have a string cell array like
>
> > > > v=3D3D{{'aaaa'}, {'bb'}, {'ccc'}, {'dd'}}
>
> > > > v =3D3D
>
> > > > =3DA0 =3DA0 {1x1 cell} =3DA0 =3DA0{1x1 cell} =3DA0 =3DA0{1x1 cell} =
=3DA0 =3DA0{1x1 cell=3D
> > }
>
> > > > and I would like to use strmatch() to located the cell array index =
in
> > > > which the string contains 'a' for example. I used below command:
>
> > > > =3DA0idx =3D3D strmatch('a', v)
> > > > ??? Error using =3D3D=3D3D> cell.strmatch at 18
> > > > Requires character array or cell array of strings as inputs.
>
> > > > K>> idx =3D3D strmatch('a', v(:))
> > > > ??? Error using =3D3D=3D3D> cell.strmatch at 18
> > > > Requires character array or cell array of strings as inputs.
>
> > > > =3DA0idx =3D3D strmatch('a', v{:})
> > > > ??? Error using =3D3D=3D3D> cell.strmatch
> > > > Too many input arguments.
>
> > > > I wrote v like above rather than v =3D3D {'aaa', 'bbb', 'cc', 'ddd'=
}
> > > > directly is because I got something like the original v above forma=
t
> > > > when I used textscan() function to read each line from a textfile a=
nd
> > > > then I need to parse the textscan results using the strmatch but me=
t
> > > > same errors as I got above.
>
> > > > Any help wil be appreciated.
>
> > > You will probabaly need to use cellfun or a loop as you have the extr=
a ce=3D
> > ll level. If you just want to know if there is an 'a' but don't care wh=
ere =3D
> > in the string it is, you could use:
>
> > > >> cellfun(@(x) ~isempty(strfind(x{1},'a')),v)
>
> > > ans =3D3D
>
> > > =3DA0 =3DA0 =3DA01 =3DA0 =3DA0 0 =3DA0 =3DA0 0 =3DA0 =3DA0 0- Hide qu=
oted text -
>
> > > - Show quoted text -
>
> > Thank you very much for the help, Stuart. It is very helpful. Here I
> > have one more question. Can you tell me why you need to use x{1}
> > instead of x directly in the strfind() function? Thanks again.
>
> In the statement: cellfun(@(x) ~isempty(strfind(x{1},'a')),v)
>
> ...cellfun takes each element of the cell array v and passes it to your a=
nonymous function, in this case @(x) ~isempty(strfind(x{1},'a')).
>
> Now, as your cell array array is in fact an array of cells, in the anonyo=
us function x is a cell. e.g.
>
> >> x=3Dv{1};
> >> whos
>
> =A0 Name =A0 =A0 =A0Size =A0 =A0 =A0 =A0 =A0 =A0Bytes =A0Class =A0 =A0Att=
ributes =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0
> =A0 v =A0 =A0 =A0 =A0 1x4 =A0 =A0 =A0 =A0 =A0 =A0 =A0 502 =A0cell =A0 =A0=
 =A0 =A0 =A0 =A0 =A0
> =A0 x =A0 =A0 =A0 =A0 1x1 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A068 =A0cell =A0 =
=A0 =A0 =A0 =A0 =A0 =A0
>
> You need to index into the cell 'x' (using {} braces) to reach the string=
.
>
> >> c=3Dx{1};
> >> whos
>
> =A0 Name =A0 =A0 =A0Size =A0 =A0 =A0 =A0 =A0 =A0Bytes =A0Class =A0 =A0Att=
ributes
> =A0 c =A0 =A0 =A0 =A0 1x4 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 8 =A0char =A0 =
=A0 =A0 =A0 =A0 =A0 =A0
> =A0 v =A0 =A0 =A0 =A0 1x4 =A0 =A0 =A0 =A0 =A0 =A0 =A0 502 =A0cell =A0 =A0=
 =A0 =A0 =A0 =A0 =A0
> =A0 x =A0 =A0 =A0 =A0 1x1 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A068 =A0cell =A0 =
=A0
>
> Stuart- Hide quoted text -
>
> - Show quoted text -

Thank you so much for your time, help and patience on this question,
Stuart.

Subject: How to match string for cell array strmatch() in Matlab

From: Ryan Ollos

Date: 4 Dec, 2008 17:22:01

Message: 6 of 6

Xiaoxiao <xiaoxiaoyang@live.com> wrote in message <bfe4a0fb-0fcd-4df2-bb9b-678743c2e104@j39g2000yqn.googlegroups.com>...
> Thank you very much for the help, Stuart. It is very helpful. Here I
> have one more question. Can you tell me why you need to use x{1}
> instead of x directly in the strfind() function? Thanks again.

You might find it less confusing if you first convert your "cell containing an array of cells" into an "array of cells":

u = [v{:}];

Then you can call Stuart's function like this:

cellfun(@(x) ~isempty( strfind(x, 'a') ), u)

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com