How to concatenate structs with different fieldnames?
Ältere Kommentare anzeigen
I'm looking for a way to concatenate structures which have different fieldnames into one struct array of with similar fieldnames.
Below you will find a minimum working example:
% Convert this
Object(1).Stats.Var1 = 1;
Object(1).Stats.Var2 = 2;
Object(2).Stats.Var1 = 1;
% Towards this
Output = [struct('Var1', 1, 'Var2', 2);
struct('Var1', 1, 'Var2', [])];
% But this doesn't work
[Object.Stats]
This returns an error message "Number of fields in structure arrays being concatenated do not match. Concatenation of structure arrays requires that these arrays have the same set of fields."
Akzeptierte Antwort
Weitere Antworten (1)
[Object(1:2).Stats]=deal( struct('Var1', [], 'Var2', [])); %first, do this
Object(1).Stats.Var1 = 1;
Object(1).Stats.Var2 = 2;
Object(2).Stats.Var1 = 1;
Output=[Object.Stats]
1 Kommentar
JBl147
am 12 Dez. 2018
Kategorien
Mehr zu Structures finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!