How can I merge structures/structure arrays in MATLAB? -


i trying merge 2 structs identical fields. tried several ways, such this , this. either turns out sideways or doesn't work @ all.

my 2 (simplified) structs are

a(1).name = 'x'; a(1).data = 1; a(2).name = 'y'; a(2).data = 2; 

and

b(1).name = 'x'; b(1).data = 3; b(2).name = 'y'; b(2).data = 4; 

the desired output identical produce:

c(1).name = 'x'; c(1).data = 1; c(2).name = 'y'; c(2).data = 2; c(3).name = 'x'; c(3).data = 3; c(4).name = 'y'; c(4).data = 4; 

what easy way this? in real struct, there more 2 fields on thousand values.

this answered tersely in a comment matthias w., i'll elaborate here...

when structures have identical fields, can treat them other object when concatenating them. solution above example be:

c = [a b]; 

since a , b in case 1-by-2 structure arrays, horizontally concatenates them larger 1-by-4 structure array. if sizes/dimensions of a , b weren't known, this:

c = [a(:).' b(:).']; 

this uses colon operator reshape them column arrays, transposes them row arrays before concatenating them.

more complicated cases...

  • merge fields across structures: this question deals case when want combine multiple structures (with same fields) single structure (not structure array). in case, each individual field being concatenated across number of structures.

  • merge different structures one: this question deals case have multiple structures different fields , want merge them 1 single structure all fields each individual structure. caveat has considered here how handle collisions: if same field appears in multiple structures, field value appears in final merged structure?


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -