Assign array in field struct
조회 수: 2 (최근 30일)
이전 댓글 표시
class(Sis)
ans =
'struct'
length(Sis)
ans =
82
c=1
2
3
..
82
i want to create new field in struct
i want this:
Sis.b(1)=1;
Sis.b(2)=2;
..
Sis.b(82)=82;
now i want to semplify calculate
c=1:82
c=c'
>> [Sis.b]=c
Insufficient number of outputs from right hand side of equal sign to satisfy assignment.
댓글 수: 3
채택된 답변
Paul
2023년 7월 9일
Example data
Sis(1).a = 1;
Sis(2).a = 2;
Sis(3).a = 3;
c = [30 40 50];
Assign elements of c to new field of elements of Sis
temp = num2cell(c);
[Sis.b] = temp{:};
Sis.b
Or in one line
clear
Sis(1).a = 1;
Sis(2).a = 2;
Sis(3).a = 3;
c = [30 40 50];
[Sis.b] = table2struct(table(c')).Var1;
Sis.b
댓글 수: 3
Walter Roberson
2023년 7월 9일
When you use table() and pass in an expression (instead of a variable name) then table() automatically uses 'Var' followed by the column number as the name of the variable. So for example table([10;20]; [30;40]) would produce a 2 x 2 table with variables 'Var1' and 'Var2'
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!