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:
How to get a field of all elements in a structure array?

Subject: How to get a field of all elements in a structure array?

From: Xiaoxiao

Date: 23 Sep, 2008 23:29:12

Message: 1 of 3

Hi,

I have a structure array like below, which I obtained from filenames =
dir(*.*),

filenames =

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

I would like to just get the name field of structure array, like 45x1
name. Can someone kindly tell me how i can do that? When I used name =
filenames.name, I only got one name. I don't want to use a loop to
obtain all the names because the structure array will be quite big
(not as in this example) in some directory.

Thanks a lot for the help in advance.

Subject: How to get a field of all elements in a structure array?

From: Walter Roberson

Date: 23 Sep, 2008 23:50:50

Message: 2 of 3

Xiaoxiao wrote:

> I have a structure array like below, which I obtained from filenames =
> dir(*.*),

> filenames =
> 45x1 struct array with fields:
> name
> date
> bytes
> isdir
> datenum I would like to just get the name field of structure array, like 45x1
> name. Can someone kindly tell me how i can do that?

As a cell array:

names = {filenames(:).name};

As a structure array:

names = struct('name', {filenames(:).name});

Subject: How to get a field of all elements in a structure array?

From: Xiaoxiao

Date: 24 Sep, 2008 13:34:25

Message: 3 of 3

On Sep 23, 7:50=A0pm, Walter Roberson <rober...@hushmail.com> wrote:
> Xiaoxiao wrote:
> > I have a structure array like below, which I obtained from filenames =
=3D
> > dir(*.*),
> > filenames =3D
> > 45x1 struct array with fields:
> > =A0 =A0 name
> > =A0 =A0 date
> > =A0 =A0 bytes
> > =A0 =A0 isdir
> > =A0 =A0 datenum I would like to just get the name field of structure ar=
ray, like 45x1
> > name. Can someone kindly tell me how i can do that?
>
> As a cell array:
>
> names =3D {filenames(:).name};
>
> As a structure array:
>
> names =3D struct('name', {filenames(:).name});

Thank you very much for the help, Walter.

Tags for this Thread

No tags are associated with 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