how to set multiple fields in a struct without using a loop

조회 수: 3 (최근 30일)
Lisandro Jimenez
Lisandro Jimenez 2014년 6월 6일
댓글: Lisandro Jimenez 2014년 6월 6일
Hello guys, I have the next problem, I want to make something like this:
A={'A1';'A2';'A3'};
them make B=somefunction(B,A); and obtain this:
B =
A1: []
A2: []
A3: []
Any idea?
  댓글 수: 4
José-Luis
José-Luis 2014년 6월 6일
B is a struct with fields A1, A2 and A3?

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

채택된 답변

José-Luis
José-Luis 2014년 6월 6일
편집: José-Luis 2014년 6월 6일
A = {'A1';'A2';'A3'};
your_struct = cell2struct(repmat({[]},3,1),A);
Please accept an answer if it helped you.
  댓글 수: 1
Lisandro Jimenez
Lisandro Jimenez 2014년 6월 6일
It's better:
A = {'A1';'A2';'A3'};
your_struct = cell2struct(repmat({[]},size(A,1),1),A);

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

추가 답변 (1개)

Lisandro Jimenez
Lisandro Jimenez 2014년 6월 6일
for i=1:size(A,1)
B.(A{i})=[];
end
But this answer has a loop, and I don't know if there's another way more efficent.

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by