Is it possible to concatenate structures with the same fields in to one super structure?

조회 수: 152 (최근 30일)
I have structures c and c1, each contain 55 fields with the same names.
The field dimensions differ slightly in the x domain (ie):
c.E: [68×120 single]
c.N_z_cross: [68×120 single]
c.N_z_long: [68×120 single]
and
c1.E: [84×120 single]
c1.N_z_cross: [84×120 single]
c1.N_z_long: [84×120 single]
ideally I would like to create a structure that contains both continually (ie)
full_data.E: [152×120 single]
full_data.N_z_cross: [152×120 single]
full_data.N_z_long: [152×120 single]
Is there anyway to do this without manually concatenating each variable?
Thanks in advance.
  댓글 수: 1
Stephen23
Stephen23 2017년 11월 5일
편집: Stephen23 2017년 11월 5일
"Is it possible to concatenate structures": yes, it is certainly possible to concatenate structures together:
[struct1,struct2]
will concatenate two structures together. But what you describe in the body of your question is how to to concatenate structure fields together, not the structures themselves. Both of these things are possible, but require very different code.

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

채택된 답변

Jan
Jan 2017년 11월 5일
편집: Stephen23 2019년 10월 2일
Or with a loop:
function S = CatStructFields(S, T, dim)
fields = fieldnames(S);
for k = 1:numel(fields)
aField = fields{k}; % EDIT: changed to {}
S.(aField) = cat(dim, S.(aField), T.(aField));
end
Then:
full_data = CatStructFields(c, c1, 1)
  댓글 수: 3
Murat Aydin
Murat Aydin 2019년 10월 2일
I had the same question, but I'm getting an error from this code, which is based on what is above, written for two structures Chain1 and Chain2 that have the same fields in the same order.
fields = fieldnames(Chain1);
for k = 1:numel(fields)
aField = fields(k);
fit.(aField) = cat(1, Chain1.(aField), Chain2.(aField));
end
Error: Argument to dynamic structure reference must evaluate to a valid field name.
Basically, it does not accept Chain1.(aField) - or using aField cell to refer to a structure field in general - as valid syntax.
Stephen23
Stephen23 2019년 10월 2일
편집: Stephen23 2019년 10월 2일
@Murat Aydin: the error is easy to indentify: fieldnames returns a cell array of character vectors, but the dynamic fieldname syntax requires a character vector. So you just need to use the correct indexing to get the character vector out of the cell array:
aField = fields{k};

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

추가 답변 (3개)

Ba Mo
Ba Mo 2019년 7월 24일
편집: Ba Mo 2019년 7월 24일
my_struct_fields = fieldnames(my_struct1);
super_struct=arrayfun(@(i) [my_struct1.(my_struct_fields{i});my_struct2.(my_struct_fields {i})],[1:numel(my_struct_fields)]','un',0);
my_dirty_trick = [my_struct_fields,super_struct]';
final_struct = struct(my_dirty_trick{:});
Thank you for officially accepting my answer

Rubens Rossi
Rubens Rossi 2019년 12월 19일
편집: Rubens Rossi 2019년 12월 19일
Thank you for the solution. I modified CatStructFields to handle my structures, which rows are 'channels'.
% Important: field position can be different between the two structures,
% but not the row position (e.g. channels), which do not have names.
S=varargin{1};
for idxV = 1:length(varargin)-1
T=varargin{idxV+1};
for k = 1:numel(F)
for h = 1:length(S)
S(h).(F{k}) = cat(dim,S(h).(F{k}),T(h).(F{k}));
end
end
end

aooEo
aooEo 2022년 10월 5일
hello, i also faced the same problem, after seeing above answers i did not build a function and use the for cycle to solve the problem, maybe it is also a idea to encourage you.
fields = fieldnames(F1);
sum_F1 = repmat(empty, numel(F1)+numel(F1_fore), 1);
for k = 1:numel(fields)
for i = 1 : numel(F1)
sum_F1(i).(fields{k}) = F1(i).(fields{k});
end
end
for k = 1:numel(fields)
for i = numel(F1)+1 : numel(F1) + numel(F1_fore)
sum_F1(i).(fields{k}) = F1_fore(i-numel(F1)).(fields{k});
end
end

카테고리

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