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

6 vues (au cours des 30 derniers jours)
Megan Ladds
Megan Ladds le 19 Jan 2018
Commenté : Megan Ladds le 23 Jan 2018
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 commentaires
Walter Roberson
Walter Roberson le 22 Jan 2018
Modifié(e) : Walter Roberson le 22 Jan 2018
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 le 23 Jan 2018
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.

Connectez-vous pour commenter.

Réponses (2)

Matt J
Matt J le 20 Jan 2018
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 commentaires
Walter Roberson
Walter Roberson le 21 Jan 2018
As I said, "concatenating the two resulting cells".
See my solution.
Matt J
Matt J le 21 Jan 2018
Well, yes, but it's not "more automatic", right? You're still using an embedded for-loop via arrayfun.

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 21 Jan 2018
Modifié(e) : Walter Roberson le 21 Jan 2018
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 commentaires
Walter Roberson
Walter Roberson le 22 Jan 2018
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 le 22 Jan 2018
Unfortunately I am away from my computer for a bit and will not be able to check until later.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by