3
votes

I'd like to achieve the following using dynamic fieldnames instead of setfield:

Say a struct 'myStruct' has a set of nested structures, i.e.

myStruct.a.b.c = 0
myStruct.a.d = 0
myStruct.a.e.f.g = 0

I want to be able to flexibly set the leaf structure values as follows:

fields = {'a', 'b', 'c'}
paramVal = 1
setfield(myStruct, fields{:}, paramVal)

This works using setfield. Is there a syntax that will do this using dynamic fieldnames? The following obviously doesn't work because the fieldname needs to be a string not an array, but demonstrates what I want:

myStruct.(fields{:}) = 0

Which would be equivalent to:

myStruct.('a').('b').('c') = 0
2
So you're assuming that myStruct.a.b.c already exists or does it need to be created as well? Is the real issue that you don't know the depth of your nested structs (structure of structures) and can't redesign your data structures? - horchler
The structure already exists, I know the depth of the structure but i want to be able to do this in a flexible way that works regardless of the depth of the particular field that I'm setting, as is done in the setfield line. I started on this path because mlint complains that i should use dynamic fieldnames instead of setfield. - Eric Schmidt
Also your call to setfield above should return myStruct or else nothing will be changed. Note that you don't get that mlint warning when you pass in a cell array. - horchler
Agreed. Was just throwing some rough code in to try to make my point. Sorry if there was any confusion. - Eric Schmidt

2 Answers

2
votes

Recursive solution without eval, ripped from one of my old utility functions:

function s = setsubfield(s, fields, val)

if ischar(fields)
    fields = regexp(fields, '\.', 'split'); % split into cell array of sub-fields
end

if length(fields) == 1
    s.(fields{1}) = val;
else
    try
        subfield = s.(fields{1}); % see if subfield already exists
    catch
        subfield = struct(); % if not, create it
    end
    s.(fields{1}) = setsubfield(subfield, fields(2:end), val);
end

I guess the try/catch can be replaced with if isfield(s, fields{1}) ..., I don't remember why I coded it like that.

Usage:

>> s = struct();
>> s = setsubfield(s, {'a','b','c'}, 55);
>> s = setsubfield(s, 'a.b.d.e', 12)
>> s.a.b.c
ans =
    55
>> s.a.b.d.e
ans =
    12
1
votes

Below is a simple, if crude, solution that works for scalar structs. Applying it to your example,

S=setfld(myStruct,'a.b.c',1)

>> S.a.b.c

ans =

     1

In general, though, deeply nested structs are unrecommended.

function S=setfld(S,fieldpath,V)
%A somewhat enhanced version of setfield() allowing one to set
%fields in substructures of structure/object S by specifying the FIELDPATH.
%
%Usage:  setfld(S,'s.f',V) will set S.s.f=V  
%                                            
%
%%Note that for structure S, setfield(S.s,'f') would crash with an error if 
%S.s did not already exist. Moreover, it would return a modified copy
%of S.s rather than a modified copy of S, behavior which would often be 
%undesirable.
%
%
%Works for any object capable of a.b.c.d ... subscripting
%
%Currently, only single structure input is supported, not structure arrays.


try
 eval(['S.' fieldpath '=V;']);
catch
 error 'Something''s wrong.';
end