Assign matrix to struct
Show older comments
Hi everyone
Can you help me for my code?.
I want to assign matrix b to struct g.a at position c.
g(1).a=[1 2 3 4]'
g(2).a=[1 1 3 4]'
g(3).a=[4 3 1 2]'
c=[1 2]
b=[ 3 3 3 3;4 5 4 4]'
My hope output is g.a =
3 4 4
3 5 3
3 4 1
3 4 2
Thank you so much.
3 Comments
Jan
on 23 Jul 2018
g.a cannot be a matrix, because g is a struct array already. Then g.a means g(:).a and it is not clear, what you want to acieve.
Rik
on 23 Jul 2018
I agree with Jan. If you want to assign vectors to specific fields, you can always use a for-loop, but how you would get your matrix out is unclear to me.
g(1).a=[1 2 3 4]';
g(2).a=[1 1 3 4]';
g(3).a=[4 3 1 2]';
c=[1 2];
b=[ 3 3 3 3;4 5 4 4]';
for n=1:numel(c)
g(c(n)).a=b(:,n);
end
Tiki Tiki
on 24 Jul 2018
Accepted Answer
More Answers (1)
Categories
Find more on Matrices and Arrays 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!