Path: news.mathworks.com!not-for-mail
From: "Stuart McGarrity" <stuart.mcgarrity.nospam@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: How to match string for cell array strmatch() in Matlab
Date: Wed, 3 Dec 2008 21:53:02 +0000 (UTC)
Organization: The MathWorks Inc
Lines: 77
Message-ID: <gh6v3u$qel$1@fred.mathworks.com>
References: <e4c5e8e5-865e-44b8-b8a2-b2f19f8edb83@20g2000yqt.googlegroups.com> <bfe4a0fb-0fcd-4df2-bb9b-678743c2e104@j39g2000yqn.googlegroups.com>
Reply-To: "Stuart McGarrity" <stuart.mcgarrity.nospam@mathworks.com>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1228341182 27093 172.30.248.38 (3 Dec 2008 21:53:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 3 Dec 2008 21:53:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 512264
Xref: news.mathworks.com comp.soft-sys.matlab:504819


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