One way to do this is to use the 'subsasgn' function, which can be used to assign a value to a subscripted field. This is demonstrated in the attached example and explained below.
For this case, where there are multiple levels of the structure, create a compound indexing expression, or an array of structures, for the input parameter, 'S'. Each structure in the array must contain the type of subscript and field name. For more information on this parameter, please see the documentation page for 'subsasgn':
To do this, first split 'fieldToChange' into a cell array using 'strsplit' and splitting on the period delimiter:
>> fieldToChangeCell = strsplit(fieldToChange, '.');
Next, create an anonymous function, 'makeSParam', that takes in a field name, 'x', and places it into a structure for the 'S' parameter format of 'subsasgn':
>> makeSParam = @(x)struct('type','.','subs',x);
You can then apply this function to each cell in 'fieldToChangeCell' using 'cellfun', resulting in the compound indexing expression, 'idx':
>> idx = cellfun(makeSParam, fieldToChangeCell);
Finally, use 'subsasgn' to set the field to the desired value of 0:
>> myStruct = subsasgn(myStruct, idx, 0);
Best Answer