How do I combine data from two structures?

조회 수: 3 (최근 30일)
T Shep
T Shep 2016년 10월 21일
편집: James Tursa 2016년 10월 21일
I have two structures that have the same number of fields (saved as .mat files). How do I add the data from every field in Structure B to the corresponding fields in Structure A? I do not want to overwrite the data in Structure A.
  댓글 수: 2
James Tursa
James Tursa 2016년 10월 21일
편집: James Tursa 2016년 10월 21일
Does the term "add the data" really mean "append the data"? As in you want to append the data from B onto A without overwriting any of the data from A? Assuming this is the case, what is in the fields and how exactly do you want them appended?
T Shep
T Shep 2016년 10월 21일
Yes. I would like to append the data without overwriting any of the data from A. The only fields that I need to append are the matrices in the structure. I want to add a row at the end of each matrix with the new data.

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

답변 (1개)

James Tursa
James Tursa 2016년 10월 21일
편집: James Tursa 2016년 10월 21일
E.g., with a loop using dynamic field names:
f = fieldnames(A);
for k=1:numel(f)
A.(f{k}) = [A.(f{k});B.(f{k})];
end
If not all of the fields contain matrices that you want appended, you could add a check. E.g.,
f = fieldnames(A);
for k=1:numel(f)
if( isnumeric(A.(f{k})) )
A.(f{k}) = [A.(f{k});B.(f{k})];
end
end

Community Treasure Hunt

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

Start Hunting!

Translated by