Cell Array to Structures

조회 수: 10 (최근 30일)
Dan Lynn
Dan Lynn 2015년 10월 18일
댓글: Walter Roberson 2015년 10월 19일
Is there a way to take a 1xM cell array and transform that into a structure array without using the function struct()?
The cell array will always be formatted this way: CellArr = {<field1 name>, {cell array of field 1 contents}, <field2 name, {cell array of field 2 contents}, etc...}

채택된 답변

Walter Roberson
Walter Roberson 2015년 10월 18일
Note: this will use struct() internally.
  댓글 수: 2
Dan Lynn
Dan Lynn 2015년 10월 18일
Is there a way to do this without using cell2struct? I managed to separate my field names and contents, so how can I do this with loops?
Walter Roberson
Walter Roberson 2015년 10월 19일
You could use dynamic field assignment.
YourStruct = [];
for K = 1 : 2: length(CellArr)
YourStruct.(CellArr{K}) = CellArr{K+1};
end
It would be stylistically better to initialize
YourStruct = struct();
but you said you wanted to not use struct().
By the way, are you aware that you can do the whole thing by using
YourStruct = struct(CellArr{:});
? Though you would have to watch out for cases where the contents included cell arrays as struct() would tend to create a structure array for those cases instead of a single struct.

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

추가 답변 (0개)

카테고리

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