From: Romain Paly on
Hi all !

I need some help about a little problem with changing
names on a struct.

I have a structure with the following
fields 'Day1', 'Day2', 'Day3'... etc

I also have a variable 'day'.

My problem is that when I want to write in "Day2" for
example in my structure, I try this :

day=2;
name=strcat('Day',day);
% so that I have the name of my field in the
variable 'name'
% and then :
strucutre.name= ...

but I'm not allowed to call a field of my stucture like
this, using a variable name.

Does someone have an idea about how I can acces the
field 'DayN' with a changing 'N' ??

Thank you

Romain
FRANCE
From: Romain Paly on
Edit :

I didn't do :
strcat('Day',day);

but:
strcat('Day',num2str(day));

sorry =D
From: Jos on
"Romain Paly" <Romain-EXT.Paly(a)sanofi-aventis.com> wrote in
message <fvn039$lqe$1(a)fred.mathworks.com>...
> Hi all !
>
> I need some help about a little problem with changing
> names on a struct.
>
> I have a structure with the following
> fields 'Day1', 'Day2', 'Day3'... etc
>
> I also have a variable 'day'.
>
> My problem is that when I want to write in "Day2" for
> example in my structure, I try this :
>
> day=2;
> name=strcat('Day',day);
> % so that I have the name of my field in the
> variable 'name'
> % and then :
> strucutre.name= ...
>
> but I'm not allowed to call a field of my stucture like
> this, using a variable name.
>
> Does someone have an idea about how I can acces the
> field 'DayN' with a changing 'N' ??
>
> Thank you
>
> Romain
> FRANCE


The following example may offer some help:

% setup structure
s.x1 = 2 ;
s.x2 = 3 ;

x = 2
name = ['x' num2str(x)]
s.(name)

Take a look at the help for dynamic field names.

hth
Jos
From: romain on
On 5 mai, 15:05, "Jos " <DEL...(a)jasenDEL.nl> wrote:
> "Romain Paly" <Romain-EXT.P...(a)sanofi-aventis.com> wrote in
> message <fvn039$lq...(a)fred.mathworks.com>...
>
>
>
>
>
> > Hi all !
>
> > I need some help about a little problem with changing
> > names on a struct.
>
> > I have a structure with the following
> > fields 'Day1', 'Day2', 'Day3'... etc
>
> > I also have a variable 'day'.
>
> > My problem is that when I want to write in "Day2" for
> > example in my structure, I try this :
>
> > day=2;
> > name=strcat('Day',day);
> > % so that I have the name of my field in the
> > variable 'name'
> > % and then :
> > strucutre.name= ...
>
> > but I'm not allowed to call a field of my stucture like
> > this, using a variable name.
>
> > Does someone have an idea about how I can acces the
> > field 'DayN' with a changing 'N' ??
>
> > Thank you
>
> > Romain
> > FRANCE
>
> The following example may offer some help:
>
> % setup structure
>   s.x1 = 2 ;
>   s.x2 = 3 ;
>
>   x = 2
>   name = ['x' num2str(x)]
>   s.(name)
>
> Take a look at the help for dynamic field names.
>
> hth
> Jos- Masquer le texte des messages précédents -
>
> - Afficher le texte des messages précédents -

Yeah thanks, I didn't know I could do it like this "struct.(field)",
now it's ok !!