Assign array in field struct
2 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Paul
on 9 Jul 2023
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 Comments
Walter Roberson
on 9 Jul 2023
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'
More Answers (1)
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!