|
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.
|