How am I using set wrong?

조회 수: 2 (최근 30일)
Josh G.
Josh G. 2020년 8월 26일
댓글: Josh G. 2020년 8월 26일
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?

채택된 답변

Fangjun Jiang
Fangjun Jiang 2020년 8월 26일
setfield(foo,'fieldName1',bar.fieldName2)
  댓글 수: 1
Josh G.
Josh G. 2020년 8월 26일
Thanks, I'd gotten so used to using set with figures and properties I forgot they were distinct from structs and fields.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by