add2struct(the_stru​ct, struct_to_add) = new_struct

Version 1.0.0.0 (895 Bytes) by Mikhail
Adds info from one struct to another, if fields of the first struct are all present in the second.
103 Downloads
Updated 14 Feb 2015

View License

S1 = ADD2STRUCT(S0,S) is used to add information from struct S to
struct S0. Implementation is strightforward: there is a loop through
fields of S; if it finds the field of S in S0, then it adds the
contents of the field to S0. Finally, the function returns updated S0
to S1.
My example of usage (pseudocode):
Loop for my data files:
load some data
make it a nice and easy to use struct
add this struct to a struct array (!!!)
make some computations on the data to add more fields
end loop
So, the most important part is line with '(!!!)' mark. If you just write my_struct_array(i) = new_data_struct, you will get this error: "Subscripted assignment between dissimilar structures." This happened because your my_struct_array has already more fields after 'make some computations on the data to add more fields' line from the code above. The way you get around this is you assign the data from each field of your new_data_struct to the same fields in your my_struct_array(i). This is what the function does.

Things to add:
- Optional argument to add a new field to S0 from S.
- If possible, make the algorythm faster and smarter.

Enjoy!

Cite As

Mikhail (2024). add2struct(the_struct, struct_to_add) = new_struct (https://www.mathworks.com/matlabcentral/fileexchange/49709-add2struct-the_struct-struct_to_add-new_struct), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2014b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Structures in Help Center and MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.0.0.0