How can I combine fields for two different structures in two different .mat files?

조회 수: 5 (최근 30일)
Hello, I am trying to combine two structures saved in two different .mat files into one structure/.mat file. I would like to end up with a new 1x1 structure containing the same fields and all the values within the fields for both original files.
My two files contain the same six fields (x, y, z, a, b, c) containing either cell or double (x,y,z are cell and a,b,c are double) with different dimensions.
I think I need to combine the fields and then create another structure from them, but I cannot figure out how to do this.
I think I may need to combine some vertically and some horizontally (1 x value vs. value x 1 dimension), which may be an issue.
Appreciate any help Thank you, Megan
  댓글 수: 3
Walter Roberson
Walter Roberson 2018년 1월 22일
편집: Walter Roberson 2018년 1월 22일
Your train is 13821 x 228 in the first one and 3411x231 in the second one. That cannot be combined in one array 17232 x 459 without padding with some value, and it is not clear how you would like the data arranged. If the two datasets are T1 and T2, do you want blkdiag(T1,T2) ? That would be the right size, but would pad after T1 with 0's as columns, and would pad before T2 with 0's as columns.
Your logic appears to be that vectors should be converted to common orientation and concatenated, but that non-vectors should be blkdiag() together ? Would that be the case even if the non-vectors have a common dimension?
I would suggest to you that your logic is getting complex enough that it would be easier (and probably faster) to handle the fields by name with the logic for that field. The logic to detect which situation is being processed is not going to allow you to vectorize without the use of an auxiliary true function, at which point you might as well just give up and code more directly.
Megan Ladds
Megan Ladds 2018년 1월 23일
Alrighty, I figured it would come to something like that. Thank you so much for your help, I'll likely use the new info I have.

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

답변 (2개)

Matt J
Matt J 2018년 1월 20일
Let s1 and s2 be the two structures. Then you can just do
f=fieldnames(s1);
for i=1:numel(f)
scombined.(f{i})={s1.(f{i}), s2.(f{i})};
end
  댓글 수: 6
Walter Roberson
Walter Roberson 2018년 1월 21일
As I said, "concatenating the two resulting cells".
See my solution.
Matt J
Matt J 2018년 1월 21일
Well, yes, but it's not "more automatic", right? You're still using an embedded for-loop via arrayfun.

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


Walter Roberson
Walter Roberson 2018년 1월 21일
편집: Walter Roberson 2018년 1월 21일
A = struct('x', 1:2, 'y', 3:7);
B = struct('x',8:10,'y', 19:22);
temp = [struct2cell(A), struct2cell(B)];
result = cell2struct(arrayfun(@(ROWIDX) [temp{ROWIDX,:}], (1:size(temp,1)).', 'uniform', 0),fieldnames(A));
  댓글 수: 4
Walter Roberson
Walter Roberson 2018년 1월 22일
Are all of the items vectors?
result = cell2struct(cellfun(@(P,Q) [P(:);Q(:)], struct2cell(A), struct2cell(B), 'uniform', 0), fieldnames(A));
Walter Roberson
Walter Roberson 2018년 1월 22일
Unfortunately I am away from my computer for a bit and will not be able to check until later.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by