Assign contents of structure into another structure with “fieldname-paths” of variable depth (dynamic fieldnames)

2 visualizzazioni (ultimi 30 giorni)
I have a structure in MATLAB, CurrStructSubset, of which the content/all fieldnames (not the structure itself!) should be added to another structure, OverViewStruct, but, the path of fieldnames within this structure where the contents of CurrStructSubset should be saved to, is variable. It can be OverViewStruct.toplevel at some run, and at another run it might be OverViewStruct.toplevel.secondlevel.thirdlevel.bottomlevel ... . An example below:
This is my structure I want to append another structure to:
OverViewStruct =
bankcontact: [1x1 struct]
food: [1x1 struct]
sociaal: [1x1 struct]
transport: [1x1 struct]
vastekosten: [1x1 struct]
This is the structure I want to append:
CurrStructSubset = getfield(OverViewStruct,fields{:});
Supsequently a particular number of fields are added to CurrStructSubset, in this case, only 'cadeau'.
CurrStructSubset =
TotalSpent: 20
LogicalSaveMatchesVec: [59x1 logical]
PercSpent: 0.029814
NormalizedSpending: 57.542
cadeau: [1x1 struct]
with fields{:} = sociaal
I don't know the names of the fieldnames of CurrStructSubset beforehand, as they are not fixed, and neither are the number of fieldnames. How can I elegantly replace the current content of e.g. OverViewStruct.sociaal with the contents of CurrStructSubset without making CurrStructSubset a fieldname itself? So I want OverViewStruct.sociaal.cadeau and not OverViewStruct.sociaal.CurrStructSubset.cadeau. Take in mind that 'field' can contain any number of fields each time the script is called ... .
setfield(OverViewStruct,fields{:},CurrStructSubset); doesn't seem to do anything

Risposte (1)

Walter Roberson
Walter Roberson il 13 Nov 2013
When I worked on something similar, I realized that what I was looking for was a "structure merge" routine. Recursively copy fields from structure2 into structure1, creating new branches as needed, overwriting items that already had values, leaving intact anything that was not mentioned in the second structure.
I have my source for the resulting routine, but I am not permitted to release it in its current form (would have to be rewritten for copyright reasons). I think, though, it is safe to say that stripped over the error checking, the easiest way to implement it is recursively.
  1 Commento
babi psylon
babi psylon il 13 Nov 2013
Modificato: babi psylon il 13 Nov 2013
hi
Thx for the reply. Actually, my question is not well asked, there are two questions
1) Suppose a structure B.(SomeFields).field1 with SomeFields being a bunch of fieldnames of any possible length: e.g. SomeFields = 'toplevel.middlevel'or SomeFields = 'toplevel.middlevel.bottomlevel'. The former syntax does not actually work in Matlab, in only works with 1 level of depth, so one fieldname. How can I assign a new field to B.(SomeFields) when SomeFields refers to e.g. 3 levels op depth? To get an existing field, I use getfield(B,SomeFields{:}) with fields being a cell array of cells or string containing all the subsequent fields for each depth level e.g.: SomeFields= {toplevel, middlevel, bottomlevel}
2) how to put contents of structures into other structures without putting the structure itself, which actually I know how to do
Relating to your answer, is your suggestion to write a recursive function related to question 1? I was hoping to solve it with just a for loop combined with a correct setfield command... . Also, I'm not familiar with recursive programming, so I cannot immediately picture this recursive function I would need to write... .

Accedi per commentare.

Categorie

Scopri di più su Structures in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by