How am I using set wrong?

2 visualizaciones (últimos 30 días)
Josh G.
Josh G. el 26 de Ag. de 2020
Comentada: Josh G. el 26 de Ag. de 2020
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?

Respuesta aceptada

Fangjun Jiang
Fangjun Jiang el 26 de Ag. de 2020
setfield(foo,'fieldName1',bar.fieldName2)
  1 comentario
Josh G.
Josh G. el 26 de Ag. de 2020
Thanks, I'd gotten so used to using set with figures and properties I forgot they were distinct from structs and fields.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Structures en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by