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 assign a variable of unkown name with function eval

Subject: how to assign a variable of unkown name with function eval

From: lily

Date: 20 Aug, 2009 03:46:03

Message: 1 of 5

My question is describled as follows:

>>c=zeros(1,5);
>>name='c';
>> eval([name(1) '=5';]);
c = 5

>> eval([name(2) '=5';]);
??? Index exceeds matrix dimensions.

Don't understand why? Or some other methods can solve this problem? Thanks!

Subject: how to assign a variable of unkown name with function eval

From: TideMan

Date: 20 Aug, 2009 04:01:17

Message: 2 of 5

On Aug 20, 3:46 pm, "lily " <mediocrity...@hotmail.com> wrote:
> My question is describled as follows:
>
> >>c=zeros(1,5);
> >>name='c';
> >> eval([name(1) '=5';]);
>
> c =  5
>
> >> eval([name(2) '=5';]);
>
> ??? Index exceeds matrix dimensions.
>
> Don't understand why? Or some other methods can solve this problem? Thanks!

There is ALWAYS a better way than using the evil eval.
But what I cannot figure out is what you are trying to do.
Perhaps you could describe it on words, rather than code?

Subject: how to assign a variable of unkown name with function eval

From: Matt Fig

Date: 20 Aug, 2009 04:04:18

Message: 3 of 5

"lily " <mediocrity111@hotmail.com> wrote in message <h6igtr$h1f$1@fred.mathworks.com>...
> My question is describled as follows:
>
> >>c=zeros(1,5);
> >>name='c';
> >> eval([name(1) '=5';]);
> c = 5
>
> >> eval([name(2) '=5';]);
> ??? Index exceeds matrix dimensions.
>
> Don't understand why?

MATLAB is telling you what is wrong. The array called 'names' only has one element. You tried to use the second element of a one element array. I think what you want is this:

c=zeros(1,5);
name='c';
eval([name '(1)=5';]);
eval([name '(2)=5';]);


But why are you using eval for such a trivial operation in the first place?

Subject: how to assign a variable of unkown name with function eval

From: lily

Date: 20 Aug, 2009 08:22:02

Message: 4 of 5

"Matt Fig" <spamanon@yahoo.com> wrote in message <h6ii01$nqf$1@fred.mathworks.com>...
> "lily " <mediocrity111@hotmail.com> wrote in message <h6igtr$h1f$1@fred.mathworks.com>...
> > My question is describled as follows:
> >
> > >>c=zeros(1,5);
> > >>name='c';
> > >> eval([name(1) '=5';]);
> > c = 5
> >
> > >> eval([name(2) '=5';]);
> > ??? Index exceeds matrix dimensions.
> >
> > Don't understand why?
>
> MATLAB is telling you what is wrong. The array called 'names' only has one element. You tried to use the second element of a one element array. I think what you want is this:
>
> c=zeros(1,5);
> name='c';
> eval([name '(1)=5';]);
> eval([name '(2)=5';]);
>
>
> But why are you using eval for such a trivial operation in the first place?

I haven't illustrated my question clearly. What I dealing with are several structure arrays,including the same fields. I want to concatenate the values of the same field of different structures into a cell array. For instance,
a1.b=1;a1.c=3;a1.d=5;
a2.b=2;a2.c=4;a2.d=6;

I want to get the values of different fields together.
b=[a1.b a2.b];
c=[a1.c a2.c];
d=[a1.d a2.d];

Here are my codes,any smarter ways to solve this problem?
datas={a1,a2};
names={'b','c','d'};
for i=1:length(datas)
    data=datas(i);
    data=data{:};
    for j=1:length(names)
        name=names(j);
        name=name{:};
        eval([name '(i)=data.(name);']);
    end
end

Subject: how to assign a variable of unkown name with function eval

From: Matt

Date: 20 Aug, 2009 08:46:02

Message: 5 of 5

"lily " <mediocrity111@hotmail.com> wrote in message <h6j13a$s80$1@fred.mathworks.com>...
 
> a1.b=1;a1.c=3;a1.d=5;
> a2.b=2;a2.c=4;a2.d=6;

Why do this? Why not organize it instead as

a(1).b=1;a(1).c=3;a(1).d=5;
 a(2).b=2;a(2).c=4;a(2).d=6;


> I want to get the values of different fields together.
> b=[a1.b a2.b];
> c=[a1.c a2.c];
> d=[a1.d a2.d];

Once you have everything in a single structure array via a(i).b etc... I can't see why you would need to group the fields into cells. They are equally accessible/manipulable as held in a structure. However, you could use struct2cell() to perform the above conversion.

Tags for 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