MATLAB: How to make a structure to be input of a function and then its updated version to be output of the function

structure

I want to make a structure to be input of a function and then do some analysis and add some fields to the structure and return the updated version of structure as the ouput of the function. I tried to use:
struct=function updatestruct(struct,...)
end
and it did not update the sruct and produced a new strucutre at the output loosing the previously stored data in the struct.

Best Answer

function new_struct = update_struct(old_struct,add_field, add_val)
new_struct = old_struct;
new_struct.(add_field) = add_val;
end
Notes:
  • "function" comes before output
  • don't use struct as a variable name