From: Rich N on
Hello,

I've got a cell array of strings and I'd like to use this to generate
fields in a structure, a bit like this:

fields = {'One', 'Two', 'Three'};
value = 5;
Root.(fields{1}).(fields{2}).(fields{3}) = value;

so I end up with
Root.One.Two.Three = 5

My question is, if I don't know how long the fields array is in
advance, is there a nice way of doing this, or do I have to loop
through each entry in fields, concatenate the strings and eval
everything?

Thanks,

Rich
From: Walter Roberson on
Rich N wrote:

> I've got a cell array of strings and I'd like to use this to generate
> fields in a structure, a bit like this:
>
> fields = {'One', 'Two', 'Three'};
> value = 5;
> Root.(fields{1}).(fields{2}).(fields{3}) = value;
>
> so I end up with
> Root.One.Two.Three = 5
>
> My question is, if I don't know how long the fields array is in
> advance, is there a nice way of doing this, or do I have to loop
> through each entry in fields, concatenate the strings and eval
> everything?

Root = subsasgn(Root, struct('type', repmat({'.'},1,length(fields)), ...
'field', fields), 5);
From: Rich N on
On 10 June, 19:20, Walter Roberson <rober...(a)hushmail.com> wrote:
> Rich N wrote:
> > I've got a cell array of strings and I'd like to use this to generate
> > fields in a structure, a bit like this:
>
> > fields = {'One', 'Two', 'Three'};
> > value = 5;
> > Root.(fields{1}).(fields{2}).(fields{3}) = value;
>
> > so I end up with
> > Root.One.Two.Three = 5
>
> > My question is, if I don't know how long the fields array is in
> > advance, is there a nice way of doing this, or do I have to loop
> > through each entry in fields, concatenate the strings and eval
> > everything?
>
> Root = subsasgn(Root, struct('type', repmat({'.'},1,length(fields)), ....
>   'field', fields), 5);

Thanks, I think you meant to type
Root = subsasgn(Root, struct('type', repmat({'.'},1,length(fields)),
'subs', fields), 5);
but that's exactly what I was looking for.
 | 
Pages: 1
Prev: question on sym2poly
Next: Fast XML reader?