Combining structs into one struct.
Hello, I have several structs that I would like to combine together into one. The issue I find is that the data for some reasons gets completely jumbled. Each time the file runs, the columns from one struct become a part of another and I cannot figure out why
CombineGLMParam(‘2_2_2’, ‘2_3_2′,’2’)
CombineGLMParam(0, ‘2_5_1′,’2’)
function CombineGLMParam(bat1,bat2,bat)
%Combine GLM parameters by putting in the bat and the file. If bat1 is not 0, open the bat1 file and create a file GLM bat.
% If it is 0, open the GLM Bat file and append the bat 2 file to GLM.
if bat1~=0
b1=load([dirf,’Bat_’,bat1,’_GLM_Params.mat’]);
b2=load([dirf,’Bat_’,bat2,’_GLM_Params.mat’]);
b1=struct2cell(b1)
b2=struct2cell(b2)
else
b1=load([dirf,’GLM_’,bat,’.mat’]);
b2=load([dirf,’Bat_’,bat2,’_GLM_Params.mat’]);
b1=struct2cell(b1.b4)
b2=struct2cell(b2)
end
for n=1:size(b2,1)
b3{n}=[b1{n}’ b2{n}’]’;
end
Header={‘ThetaX’, ‘ThetaY’, ‘ThetaZ’, ‘Td’, ‘Vd’, ‘Vw’, ‘Hd’, ‘Hw’, ‘Ld’,’Lw’};
b4=cell2struct(b3, Header, 2)
save([dirf,’GLM_’,bat,’.mat’], ‘b4’)Hello, I have several structs that I would like to combine together into one. The issue I find is that the data for some reasons gets completely jumbled. Each time the file runs, the columns from one struct become a part of another and I cannot figure out why
CombineGLMParam(‘2_2_2’, ‘2_3_2′,’2’)
CombineGLMParam(0, ‘2_5_1′,’2’)
function CombineGLMParam(bat1,bat2,bat)
%Combine GLM parameters by putting in the bat and the file. If bat1 is not 0, open the bat1 file and create a file GLM bat.
% If it is 0, open the GLM Bat file and append the bat 2 file to GLM.
if bat1~=0
b1=load([dirf,’Bat_’,bat1,’_GLM_Params.mat’]);
b2=load([dirf,’Bat_’,bat2,’_GLM_Params.mat’]);
b1=struct2cell(b1)
b2=struct2cell(b2)
else
b1=load([dirf,’GLM_’,bat,’.mat’]);
b2=load([dirf,’Bat_’,bat2,’_GLM_Params.mat’]);
b1=struct2cell(b1.b4)
b2=struct2cell(b2)
end
for n=1:size(b2,1)
b3{n}=[b1{n}’ b2{n}’]’;
end
Header={‘ThetaX’, ‘ThetaY’, ‘ThetaZ’, ‘Td’, ‘Vd’, ‘Vw’, ‘Hd’, ‘Hw’, ‘Ld’,’Lw’};
b4=cell2struct(b3, Header, 2)
save([dirf,’GLM_’,bat,’.mat’], ‘b4’) Hello, I have several structs that I would like to combine together into one. The issue I find is that the data for some reasons gets completely jumbled. Each time the file runs, the columns from one struct become a part of another and I cannot figure out why
CombineGLMParam(‘2_2_2’, ‘2_3_2′,’2’)
CombineGLMParam(0, ‘2_5_1′,’2’)
function CombineGLMParam(bat1,bat2,bat)
%Combine GLM parameters by putting in the bat and the file. If bat1 is not 0, open the bat1 file and create a file GLM bat.
% If it is 0, open the GLM Bat file and append the bat 2 file to GLM.
if bat1~=0
b1=load([dirf,’Bat_’,bat1,’_GLM_Params.mat’]);
b2=load([dirf,’Bat_’,bat2,’_GLM_Params.mat’]);
b1=struct2cell(b1)
b2=struct2cell(b2)
else
b1=load([dirf,’GLM_’,bat,’.mat’]);
b2=load([dirf,’Bat_’,bat2,’_GLM_Params.mat’]);
b1=struct2cell(b1.b4)
b2=struct2cell(b2)
end
for n=1:size(b2,1)
b3{n}=[b1{n}’ b2{n}’]’;
end
Header={‘ThetaX’, ‘ThetaY’, ‘ThetaZ’, ‘Td’, ‘Vd’, ‘Vw’, ‘Hd’, ‘Hw’, ‘Ld’,’Lw’};
b4=cell2struct(b3, Header, 2)
save([dirf,’GLM_’,bat,’.mat’], ‘b4’) struct, structure MATLAB Answers — New Questions