Is it possible to add a matrix to a structure 'column' without using a for-loop? For example I have a structure with 3 fields
A.name
A.grade
A.attendance
now A.attendance
expects a 1x5 matrix. If I have a 5x5 matrix, can I directly insert it into 5 rows of the structure A? something like
A(1:5).attendance = B
where B is a 5x5 matrix
If your B
is actually a 5 element cell array where each element is a 1-by-5 matrix (actually each element can contain anything), then
[A.attendance] = B{:}
will work. You can convert your 5-by-5 double matrix B
to the desired form as follows:
B_cell = mat2cell(B, ones(size(B,1),1),size(B,2))
or skip the temp variable and use deal
:
[A.attendance] = deal(mat2cell(B, ones(size(B,1),1),size(B,1)))
You can convert B
to a cell array of its rows,
C = mat2cell(B, ones(size(B,1),1), size(B,2))
and then you can assign as follows
[A(1:size(B,2)).attendance] = C{:};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With