Structure Assginment

조회 수: 8 (최근 30일)
Umit
Umit 2012년 5월 8일
I need a better approach to assign matrix to a structure. Currently I have
A = 5 by 1 struct with Loc & Err variables, A(1:5).Loc & A(1:5).Err,
I have Values matrix 5 by 6 where Loc info stored.
My code;
for i=1:5
A(i).Loc = Values(i,:)
end
Is there easy way of assignment. I have a very complex structure, this is very simplified version of it. Any ideas??
  댓글 수: 1
Daniel Shub
Daniel Shub 2012년 5월 8일
This seems pretty easy to understand for me. What do you mean by easy?

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2012년 5월 8일
Try this:
vcell = mat2cell(Values, ones(1, size(Values,1)), size(Values,2));
[A.Loc] = vcell{:};
  댓글 수: 4
Daniel Shub
Daniel Shub 2012년 5월 8일
If there are lots of fields you might want to consider cell2struct
Umit
Umit 2012년 5월 8일
It works, thank you. This is what I want

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2012년 5월 8일
There is no easy way to assign a very complicated structure.
Using a loop is most likely the easiest and most efficient method, especially if you use implicite pre-allocation by running the loop from the end to the start.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by