add2struct(the_struct, struct_to_add) = new_struct
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
Platform Compatibility
Windows macOS LinuxCategories
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.
Version | Published | Release Notes | |
---|---|---|---|
1.0.0.0 |