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

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com