I am writing a matlab class and would like to overwrite the subasgn operator to achieve this
obj.('elem1').('subelem1').('subsubelem1')=val;
where the level of depth can vary. This type of multi-level assignment is supported in standard struct (but not in containers.Map or dictionary).
from the below link, I found examples for writing a subsasgn for only 1 subfield level, but it does not support multiple levels as struct does
https://www.mathworks.com/help/matlab/matlab_oop/code-patterns-for-subsref-and-subsasgn-methods.html
I am wondering if anyone can give some pointers on how to achieve this?
thanks
Update: [09/26/2024]
my sample code can be found in https://github.com/fangq/jsonlab/blob/master/jdict.m
specifically, the subsref() function can retrieve multi-level subkeys on both MATLAB and Octave.
however, the subsasgn function can only modify the value for only one level of subfield
Update: [09/28/2024]
following the code sample suggested by rahnema1, the subsasgn in my jdict class can set subkey values at any level, and also allows to append new keys. the code can be found at
https://github.com/fangq/jsonlab/blob/ade19191e48ec3b76c362a00f696e959cd6fcdf8/jdict.m#L170-L236
sinput argument is a struct array (everything is an array in MATLAB). Each element of this array gives you one level of indexing. In your example the array would have 3 elements. You’ll have to figure out how to parse those elements to accomplish your goal. Use the debugger to look at what thisslooks like under various indexing operations.sinput. just to be clear, I am not trying to modify multiple elements in the same level, but multiple levels.subasgnalready exposes the operators and operands, but the problem is that it won't allow recursive assignment.subsasgnandsubsref, so I figured you know about those. Both have an input arguments. Each level of indexing is an element in this arrays.s(1)is the first level of indexing.s(2)is the second level of indexing. Etc. You don’t need recursion.obj.('elem1')is the same asobj.elem1. The parenthesis syntax is meant for when the indexing key is a variable:var = 'elem1'; obj.(var).