Is it possible to create a nested structure with dynamic field names?

6 views (last 30 days)
I am parsing an ascii data header file with obnoxiously human unreadable field names like:
structureName.manufacturer.modelNumber.serialNumber.hardwarePartID(6).hardwareAddresses(24).featureList(274).licensed = "1"
There are thousands of lines, and since this needs to be inspected by a human from time to time, I am reorganizing the information into a more readable and workable format, but I have run into a problem. I need only certain lines of data, and only if those lines exist. I have settled upon making a variable list of parameters I need (based on the specific application), and I have read the ascii text into structures with the same field names. The problem is when I have to assign these parsed parameter values into my new (readable) structure, I have to step through the list of field names.
I would like to be able to use dynamic field naming to make the assignments. For instance,
fieldname = 'charles'; %this name would change with every step, and itself will be nested as well
structureName.(fieldname) = parsedStructure.(manufacturer).(modelNumber). ... %etc
However, this version of dot indexing (right side dynamic naming) is not supported. Neither is:
structureName.(fieldname) = parsedStructure.(manufacturer.modelNumber) ... %etc
And, of course, the first thing I tried was using the dotted fieldnames as text:
parsedField = 'manufacturer.modelNumber.whateverNonsenseComesAfterThis';
structureName.(fieldname) = parsedStructure.(parsedField);
But using nested field names isn't supported either. Or at least, I haven't been able to figure out how to do it.
I can make it work with an eval call, but there are several reasons why that isn't a good idea, and I'd rather not stick with that solution. Basically, what I need to do is be able to step through a list of (hundreds of) required parameters, and for those that exist in the data, extract them into my new structure.
Any ideas?
Jeffrey Luci
Jeffrey Luci on 27 Jan 2023
Edited: Jeffrey Luci on 27 Jan 2023
I didn't overlook that. You can't get or set nested fieldnames with those functions.
f = 'one.two.three';
fSplit = split(f, '.');
hdr = setfield(hdr, fSplit, 1);
%This isn't supported, either:
hdr = setfield(hdr, f, 1);

Sign in to comment.

Accepted Answer

Stephen23 on 20 Jan 2023
Edited: Stephen23 on 20 Jan 2023
"However, this version of dot indexing (right side dynamic naming) is not supported."
Lets try it right now:
S.A.B.C = pi;
ans = 3.1416
If you want to mix in subscript indexing as well then you can use GETFIELD() / SETFIELD():
S.A.B(2).C = exp(1);
ans = 2.7183
Another option is to use SUBSREF() / SUBSASGN():
X = substruct('.','A', '.','B', '()',{2}, '.','C');
ans = 2.7183
Obviously you just need to use STRSPLIT() or similar to get each level of the structure heirarchy from your single piece of text, and handle those subscript indices correctly. You might find cell arrays and comma-separated lists very useful:

More Answers (0)


Find more on Structures in Help Center and File Exchange




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!