How to recursively add fields in a structure.

Hey there!
I am trying to recursively add results into a single structure from the following piece of code: http://www.mathworks.com/matlabcentral/fileexchange/18160-evaluate-nelson-siegel-function/content/nelson/nelsonfit.m
This function returns a structure with two field which have dimensions of 1x1 and 4x1 respectively. Instead of the input y, which in the example is a 1x11 vector, I would like to insert an array with multiple rows. I thought I should write a different function which repeats the nelsonfit function with as input y each time a different row of matrix X. That is not the problem, however I am stuck with adding the returned values of the nelsonfit function. I can add the structures but it creates a 1x2 structure while I need a 1x1 structure where the fields are simply concatenated.
Any tips or hints would be much appreciated!
Kind regards,
Michael

 Risposta accettata

How are you concatenating the fields? If I run nelsonfit with two different sets of inputs, the function returns two structures with the tau and beta fields of dimensions 1x1 and 3x1 respectively. If par1 and par2 are the two returned structures, then I can create one as follows:
parAll.tau = [par1.tau par2.tau];
parAll.beta = [par1.beta par2.beta];
with parAll now being a single structure
parAll =
tau: [2.7680 1.1280]
beta: [3x2 double]
Appending the third structure is just as easy
parAll.tau = [parAll.tau par2.tau];
parAll.beta = [parAll.beta par2.beta];
Try the above and see what happens!

4 Commenti

Michael's answer moved here
Thank you very much for your reply! I was wondering if it is also possible to concatenate multiple structs without mentioning their fieldnames. Such that all their fields are concatenated automatically.
Thank you!
Michael - please remove your Answers as they are not answers to the question but rather responses to an a answer (to your question) or a clarification of some kind. Use the Comment fields to continue the dialog. (That way, others with a similar problem aren't confused by all the answers.)
As for concatenating multiple structs without mentioning their field names, try experimenting with struct2cell and then cell2struct. If all of the fields, of the two (or more) structures, are numeric then you can convert each to a cell (which will remove the field names) and then convert the cell to matrix. Each new matrix can then be concatenated. There may be quicker ways to do this, but it is a start.
Thank you! I found a different method. I extracted the names with fieldnames() to a separate variable and then iteratively added them like:
for i = 1:length(fieldname(variable1)) variable.(fieldname{i}) = [variable1.(fieldname{i}) variable2.(fieldname{i})] end for anyone interested.
That is a neat solution!

Accedi per commentare.

Più risposte (1)

Michael
Michael il 13 Giu 2014
I already just changed the output to two separate array's so I could just concatenate the array's with a for loop. Should have thought of this earlier....

Community Treasure Hunt

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

Start Hunting!

Translated by