Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

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

short version:

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 op 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}

long version:

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

tags: matlab, structure, fieldnames, dynamic, setfield

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...