Is it possible to create a nested structure with dynamic field names?
Afficher commentaires plus anciens
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?
6 commentaires
Jeffrey Luci
le 23 Jan 2023
"I tried to employ MATLAB's new used of double quotes to reference the dynamic field names like they do in the examples for dynamic field naming. That doesn't work"
S.A = pi,
S.("A")
Both scalar strings and char vectors should work in dynamic field name indexing.
a = struct('b', struct('c', 5));
s = "b";
c = 'c';
y = a.(s).(c)
Can you show a small example where you tried to use a scalar string and it didn't work? Did you receive an error message, and if so what was the full and exact text of that error?
I could see this failing if you had a non-scalar string, but the error message seems to fairly clearly identify the cause of the error.
d = [s s]
y = a.(d)
d is a string but it's not scalar.
Jeffrey Luci
le 27 Jan 2023
"Is there a technique I have overlooked?"
As I wrote in my answer, you can use GETFIELD(), probably with a comma-separated list:
And on the LHS basic dynamic fieldname:
S.A.B.C = pi;
F = 'A.B.C';
C = split(F,'.');
Z.('new') = getfield(S,C{:})
Jeffrey Luci
le 27 Jan 2023
Modifié(e) : Jeffrey Luci
le 27 Jan 2023
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!