필터 지우기
필터 지우기

Making one structure out of two structures

조회 수: 1 (최근 30일)
g
g 2018년 11월 27일
댓글: g 2018년 11월 27일
Let's say I have two structures.
D = [strtrim(C{1}),num2cell(C{2})].';
F = struct(D{:});
% other code that creates C1
D1 = [strtrim(C1{1}),num2cell(C1{2})].'; \
E = struct(D1{:});
I now want to create a larger structure out of F and E that has the same dimensions.
So , for dummy variables, let's say F looks like (but is not limited to):
A: apple
B: ball
and E looks like:
C: cat
D: dog
How do I combine these two into:
A: apple
B: ball
C: cat
D: dog
Thanks!
  댓글 수: 1
Stephen23
Stephen23 2018년 11월 27일
Note that you could also use cell2struct to create those structures, which avoids having to create the intermediate cell arrays.

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

채택된 답변

Stephen23
Stephen23 2018년 11월 27일
편집: Stephen23 2018년 11월 27일
A general solution for two identically-sized structures, with any number of fields:
>> F.A = 'apple';
>> F.B = 'ball';
>> E.C = 'cat';
>> E.D = 'dog';
>> Z = cell2struct([struct2cell(E);struct2cell(F)], [fieldnames(E);fieldnames(F)], 1)
Z =
C = cat
D = dog
A = apple
B = ball

추가 답변 (1개)

Walter Roberson
Walter Roberson 2018년 11월 27일
Either loop setting field by field or else use struct2cell on both and combine into one cell and cell2struct back into struct.

카테고리

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