MATLAB: How am I using set wrong

fieldMATLABsetstruct

I'm trying to use set to copy a set of fields from one struct to another. Here's a minimal (non)working example of what I'm trying to do:
foo = struct('fieldName1',[]);
bar = struct('fieldName2',[10 20 30]);
set(foo,'fieldName1',bar.fieldName2)
This gives an error:
Error using set
Conversion to double from struct is not possible.
But if I do
foo.fieldName1 = bar.fieldName2
the assignment is successful. However, this method is tedious for large numbers of fields.
How am I using set wrong here?

Best Answer

setfield(foo,'fieldName1',bar.fieldName2)