|
"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
|