Assign Field to Structure Array without a Loop
이전 댓글 표시
In my routine I create a structure array within a loop like this:
Field1 = [1 2 3];
Field2 = [6 3 1];
for Runner = 1:3
StuctArray(Runner).Field1 = Field1(Runner);
StuctArray(Runner).Field2 = Field2(Runner);
end
As results, in the Variables Window the Structure Array columns are labelled with the field names, which is very convenient to me. However I try in different ways to do it without the loop, but did not succeed. E.g. I tried the following:
StructArray(1:3).Field1 = Field1
I would appreciate any hint how this can be solved without using a loop.
채택된 답변
추가 답변 (1개)
Andrei Bobrov
2018년 10월 26일
편집: Andrei Bobrov
2018년 10월 26일
cellarray = num2cell([Field1(:),Field2(:)]);
namefields = {'Field1','Field2'};
StructArray = cell2struct(cellarray,namefields,2);
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!