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

6 ビュー (過去 30 日間)
Megan Ladds
Megan Ladds 2018 年 1 月 19 日
コメント済み: Megan Ladds 2018 年 1 月 23 日
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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by