How do I assign a value to a field in a multi-level struct when the field name is contained in a variable?
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
MathWorks Support Team
le 22 Oct 2019
Modifié(e) : Stephen23
le 17 Jan 2023
I have a multi-level struct and I would like to change the value for a field in one of the lower levels. The path to the field name is stored in a char array. For example,
>> fieldToChange = 'levelA.levelB.levelC.myField';
Usually, when the name of a field is stored in a variable, I can access it using parenthetical notation:
>> fieldName = 'levelA';
>> myStruct.(fieldName) = 0;
However, when I use that notation with 'fieldToChange', I see the error below:
>> fieldToChange = 'levelA.levelB.levelC.myField';
>> myStruct.(fieldToChange) = 0;
Reference to non-existent field 'levelA.levelB.levelC.myField'
How do I access the field at the location stored in 'fieldToChange'?
Réponse acceptée
MathWorks Support Team
le 22 Oct 2019
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);
1 commentaire
Stephen23
le 17 Jan 2023
"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':"
The standard MATLAB approach is to simply call the inbuilt SUBSTRUCT() function:
Plus de réponses (1)
Stephen23
le 13 Jan 2023
Modifié(e) : Stephen23
le 17 Jan 2023
You do not need any obfuscated fiddling around with SUBSASGN().
The standard MATLAB approach is to use SETFIELD() / GETFIELD():
F = 'levelA.levelB.levelC.myField';
V = pi; % value or array
S = struct()
C = split(F,'.');
S = setfield(S,C{:},V)
Checking:
S.levelA.levelB.levelC.myField
And for completeness, the reverse operation:
W = getfield(S,C{:})
0 commentaires
Voir également
Catégories
En savoir plus sur Structures dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!