Combining rows of each field within a structure

33 views (last 30 days)
Hello,
I'd like to combined the rows of a structure for each field. I won't always know how many rows there will be in the structure. The example I show is a 1 x 3 structure, but I'd like to plan for a 1 x ? structure. So I thought combining the rows in a for loop would be the best way. I'm aiming for a 3 x 101 double in each of the fields in this example.
I'm sure there's a pretty easy solution to this but I'm a novice when it comes to MATLAB
Any help would be greatly appreciated!
  2 Comments
Image Analyst
Image Analyst on 30 Nov 2022
You forgot to attach your variable in a .mat file. Perhaps try vertcat.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
Josh Tome
Josh Tome on 30 Nov 2022
Edited: Josh Tome on 30 Nov 2022
Here is my .mat file. I am importing my data directly from another software program that works directly with MATLAB. As you can see from above the structure I'm working with is name "ModOutRstance_interp_x_all" (I know that's a long name). I've also attached the code I have up to this point.

Sign in to comment.

Accepted Answer

Voss
Voss on 30 Nov 2022
S = load('Combined_Structure.mat','ModOutRstance_interp_x_all');
new_struct = struct();
f = fieldnames(S.ModOutRstance_interp_x_all);
for ii = 1:numel(f)
new_struct.(f{ii}) = vertcat(S.ModOutRstance_interp_x_all.(f{ii}));
end
disp(new_struct);
LHipAngles: [3×101 double] LKneeAngles: [3×101 double] LAbsAnkleAngle: [3×101 double] LAnkleAngles: [3×101 double] RHipAngles: [3×101 double] RKneeAngles: [3×101 double] RAnkleAngles: [3×101 double] RAbsAnkleAngle: [3×101 double] LPelvisAngles: [3×101 double] RPelvisAngles: [3×101 double] LFootProgressAngles: [3×101 double] RFootProgressAngles: [3×101 double] RNeckAngles: [3×101 double] LNeckAngles: [3×101 double] RSpineAngles: [3×101 double] LSpineAngles: [3×101 double] LShoulderAngles: [3×101 double] LElbowAngles: [3×101 double] LWristAngles: [3×101 double] RShoulderAngles: [3×101 double] RElbowAngles: [3×101 double] RWristAngles: [3×101 double] RThoraxAngles: [3×101 double] LThoraxAngles: [3×101 double] RHeadAngles: [3×101 double] LHeadAngles: [3×101 double] LHipPower: [3×101 double] LKneePower: [3×101 double] LAnklePower: [3×101 double] RHipPower: [3×101 double] RKneePower: [3×101 double] RAnklePower: [3×101 double] LWaistPower: [3×101 double] RWaistPower: [3×101 double] LNeckPower: [3×101 double] RNeckPower: [3×101 double] LShoulderPower: [3×101 double] RShoulderPower: [3×101 double] LElbowPower: [3×101 double] RElbowPower: [3×101 double] LWristPower: [3×101 double] RWristPower: [3×101 double] LGroundReactionForce: [3×101 double] LNormalisedGRF: [3×101 double] RGroundReactionForce: [3×101 double] RNormalisedGRF: [3×101 double] LAnkleForce: [3×101 double] RAnkleForce: [3×101 double] RKneeForce: [3×101 double] LKneeForce: [3×101 double] RHipForce: [3×101 double] LHipForce: [3×101 double] LWaistForce: [3×101 double] RWaistForce: [3×101 double] LNeckForce: [3×101 double] RNeckForce: [3×101 double] LShoulderForce: [3×101 double] RShoulderForce: [3×101 double] LElbowForce: [3×101 double] RElbowForce: [3×101 double] LWristForce: [3×101 double] RWristForce: [3×101 double] LGroundReactionMoment: [3×101 double] RGroundReactionMoment: [3×101 double] LAnkleMoment: [3×101 double] RAnkleMoment: [3×101 double] RKneeMoment: [3×101 double] LKneeMoment: [3×101 double] RHipMoment: [3×101 double] LHipMoment: [3×101 double] LWaistMoment: [3×101 double] RWaistMoment: [3×101 double] LNeckMoment: [3×101 double] RNeckMoment: [3×101 double] LShoulderMoment: [3×101 double] RShoulderMoment: [3×101 double] LElbowMoment: [3×101 double] RElbowMoment: [3×101 double] LWristMoment: [3×101 double] RWristMoment: [3×101 double] CentreOfMass: [3×101 double] CentreOfMassFloor: [3×101 double] PEL: [3×101 double] LFE: [3×101 double] LTI: [3×101 double] LFO: [3×101 double] LTO: [3×101 double] RFE: [3×101 double] RTI: [3×101 double] RFO: [3×101 double] RTO: [3×101 double] HED: [3×101 double] LCL: [3×101 double] RCL: [3×101 double] TRX: [3×101 double] LHU: [3×101 double] LRA: [3×101 double] LHN: [3×101 double] RHU: [3×101 double] RRA: [3×101 double] RHN: [3×101 double]

More Answers (2)

Fangjun Jiang
Fangjun Jiang on 30 Nov 2022
s(1).f=1:3;
s(2).f=4:6;
s(3).f=7:9
s = 1×3 struct array with fields:
f
vertcat(s.f)
ans = 3×3
1 2 3 4 5 6 7 8 9

Image Analyst
Image Analyst on 30 Nov 2022
How about this:
s = load('Combined_Structure.mat');
Warning: Could not find appropriate function on path loading function handle C:\Users\jtome\Box\Haddas' Lab\Code\Gait\NexusIntegrationRstanceOnly.m>@(f)interp1(f.',linspace(1,size(f,2),101)).'
Warning: Variable 'vicon' originally saved as a ViconNexus cannot be instantiated as an object and will be read in as a uint32.
m = s.ModOutRstance_interp_x_all;
allm = vertcat(m.LHipAngles)
allm = 3×101
-14.0682 -14.3389 -14.5825 -14.7945 -14.9404 -15.0484 -15.1047 -15.0802 -15.0087 -14.8645 -14.6335 -14.3494 -13.9747 -13.5165 -13.0028 -12.3875 -11.7001 -10.9591 -10.1156 -9.2170 -8.2713 -7.2345 -6.1617 -5.0469 -3.8735 -2.6766 -1.4498 -0.1949 1.0709 2.3497 -14.3670 -14.6213 -14.8433 -15.0324 -15.1529 -15.2237 -15.2487 -15.1905 -15.0656 -14.8849 -14.6138 -14.2617 -13.8483 -13.3452 -12.7522 -12.0977 -11.3619 -10.5357 -9.6541 -8.7067 -7.6789 -6.6084 -5.4921 -4.3166 -3.1153 -1.8892 -0.6322 0.6328 1.9051 3.1782 -14.5355 -14.8635 -15.1678 -15.4468 -15.6655 -15.8477 -15.9887 -16.0462 -16.0559 -16.0077 -15.8592 -15.6543 -15.3778 -14.9932 -14.5480 -14.0230 -13.3926 -12.7034 -11.9342 -11.0730 -10.1605 -9.1770 -8.1235 -7.0312 -5.8852 -4.6957 -3.4823 -2.2378 -0.9753 0.2962

Categories

Find more on Structures in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!