Thread Subject: Variable name of struct element

Subject: Variable name of struct element

From: Steffen

Date: 16 Dec, 2007 17:05:39

Message: 1 of 3

hi there,

I´m trying to name a struct array with variable names.
For instance:

file=['Image1', 'Image2'];
Data.single_images.file=ones(512,512);

'file' should now be variable containing the different
strings of the filename. However, I don´t know the correct
syntax to do that since Matlab does not recognize 'file' as
a variable. It always saves it as 'data.single_images.file'
instead of 'data.single_images.Image1'.
Is it possible to save it that way? Any hint is much
appreciated!!

Thanks and best regards,
Steffen

Subject: Variable name of struct element

From: Bruno Luong

Date: 16 Dec, 2007 17:16:05

Message: 2 of 3

"Steffen " <rileksn@gmail.com> wrote in message
<fk3lt3$cj3$1@fred.mathworks.com>...
> hi there,
>
> I´m trying to name a struct array with variable names.
> For instance:
>
> file=['Image1', 'Image2'];
> Data.single_images.file=ones(512,512);
>
> 'file' should now be variable containing the different
> strings of the filename. However, I don´t know the correct
> syntax to do that since Matlab does not recognize 'file' as
> a variable. It always saves it as 'data.single_images.file'
> instead of 'data.single_images.Image1'.
> Is it possible to save it that way? Any hint is much
> appreciated!!
>
> Thanks and best regards,
> Steffen

Use "dynamic fieldname" (the fieldname is evaluated as
expression).

file={'Image1', 'Image2'}; % <- use cell to scope with
                           % various fieldname lengths
Data.single_images.(file{1}) = ones(512,512);
...


Bruno

Subject: Variable name of struct element

From: Steffen

Date: 18 Dec, 2007 20:59:50

Message: 3 of 3

Hi Bruno,

thanks for the hint. Works perfectly fine!

Cheers,
Steffen

Tags for this Thread

Everyone's Tags:

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.

Tag Activity for This Thread
Tag Applied By Date/Time
struct array Steffen 16 Dec, 2007 12:10:00
rssFeed for this Thread

Contact us at files@mathworks.com