Thread Subject: How to get elements in a structure array according to a structure

Subject: How to get elements in a structure array according to a structure

From: Xiaoxiao

Date: 1 Dec, 2008 18:21:52

Message: 1 of 5

Hello everyone:

I have a question on how to get elements in a structure array
according to one structure filed value in Matlab. For example, I used
the below command

names = dir(dirname);

to get the sub-directory and file names under the directory dirname.
and the results are like:

names =

7x1 struct array with fields:
    name
    date
    bytes
    isdir
    datenum

I only want to get the directory names under this directory, so I
think I can use the structure filed isdir to decide which element in
the structure array should be kept. But I don't know how to do this
without using a loop. I tried to use: idx = find(names.isdir) to get
the indices of the sub-direcotry names, but got errors from Matlab.
Can anyone kindly help me how to do this?

Thank you very much.

Subject: How to get elements in a structure array according to a structure field value in Matlab?

From: Guoliang Zhang

Date: 1 Dec, 2008 18:36:02

Message: 2 of 5

a(find([a.isdir]))


"Xiaoxiao" <xiaoxiaoyang@live.com> wrote in message
news:29239df7-3389-4d16-9946-d06cfcb4bb82@f3g2000yqf.googlegroups.com...
> Hello everyone:
>
> I have a question on how to get elements in a structure array
> according to one structure filed value in Matlab. For example, I used
> the below command
>
> names = dir(dirname);
>
> to get the sub-directory and file names under the directory dirname.
> and the results are like:
>
> names =
>
> 7x1 struct array with fields:
> name
> date
> bytes
> isdir
> datenum
>
> I only want to get the directory names under this directory, so I
> think I can use the structure filed isdir to decide which element in
> the structure array should be kept. But I don't know how to do this
> without using a loop. I tried to use: idx = find(names.isdir) to get
> the indices of the sub-direcotry names, but got errors from Matlab.
> Can anyone kindly help me how to do this?
>
> Thank you very much.

Subject: How to get elements in a structure array according to a structure

From: Xiaoxiao

Date: 1 Dec, 2008 18:41:18

Message: 3 of 5

On Dec 1, 1:36=A0pm, "Guoliang Zhang" <zha...@mathworks.com> wrote:
> a(find([a.isdir]))
>
> "Xiaoxiao" <xiaoxiaoy...@live.com> wrote in message
>
> news:29239df7-3389-4d16-9946-d06cfcb4bb82@f3g2000yqf.googlegroups.com...
>
> > Hello everyone:
>
> > I have a question on how to get elements in a structure array
> > according to one structure filed value in Matlab. For example, I used
> > the below command
>
> > names =3D dir(dirname);
>
> > to get the sub-directory and file names under the directory dirname.
> > and the results are like:
>
> > names =3D
>
> > 7x1 struct array with fields:
> > =A0 =A0name
> > =A0 =A0date
> > =A0 =A0bytes
> > =A0 =A0isdir
> > =A0 =A0datenum
>
> > I only want to get the directory names under this directory, so I
> > think I can use the structure filed isdir to decide which element in
> > the structure array should be kept. But I don't know how to do this
> > without using a loop. I tried to use: idx =3D find(names.isdir) to get
> > the indices of the sub-direcotry names, but got errors from Matlab.
> > Can anyone kindly help me how to do this?
>
> > Thank you very much.

Thank you very much for the help, Guoliang.

Subject: How to get elements in a structure array according to a structure

From: Walter Roberson

Date: 1 Dec, 2008 18:49:33

Message: 4 of 5

Guoliang Zhang wrote:
> a(find([a.isdir]))

better is

a([a.isdir])

Subject: How to get elements in a structure array according to a structure

From: Xiaoxiao

Date: 1 Dec, 2008 19:04:03

Message: 5 of 5

On Dec 1, 1:49=A0pm, Walter Roberson <rober...@hushmail.com> wrote:
> Guoliang Zhang wrote:
> > a(find([a.isdir]))
>
> better is
>
> a([a.isdir])

Good suggestion. Thanks again.

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