Finding the maximum values of four variables and nine participants

9 views (last 30 days)
Hello,
I have data for nine participants and four different variables (Right Knee, Left Knee, Right Ankle and Left Ankle) which I have imported into matlab.
I want to find the maximum value for each variable and each participant. I have written the following script:
for joint_num = 1:4
if joint_num == 1
joint = 'LAnkle';
elseif joint_num == 2
joint = 'RAnkle';
elseif joint_num == 3
joint = 'LKnee';
elseif joint_num == 4
joint = 'RKnee';
File = ['TorqueData_',joint,'_Isom'];
MaxT = max (TorqueData_LAnkle_Isom);
MaxT = max (TorqueData_RAnkle_Isom);
MaxT = max (TorqueData_LKnee_Isom);
MaxT = max (TorqueData_RKnee_Isom);
end
end
However, when I run the script, instead of getting a table of four rows and nine columns with the maximum values of each variable, I get one row of nine columns, with the maximum values of the last variable (RKnee).
I'm sure this is something simple but I am new to matlab and really struggling! If anyone could helpt it would be very much appreciated.
Thanks and Best Wishes

Answers (2)

Torsten
Torsten on 29 Jul 2015
1. The first "end" should be behind "joint = 'RKnee';".
2. The MaxT variable only contains the maximum of "TorqueData_RKnee_Isom" because you overwrite the other values obtained by the three preceding MaxT definitions. You should calculate the MaxT values inside the first if/elseif construction:
if joint_num == 1
joint = 'LAnkle';
MaxT_LAnkle = Max(TorqueData_LAnkle_Isom);
elseif ...
Best wishes
Torsten.
  1 Comment
Laura Judson
Laura Judson on 29 Jul 2015
Hello and thank you for taking the time to get back to me so quickly.
Unfortunately I have just tried the adjustment that you suggested and have received the same data (One row of nine columns with maximum values for RKnee only).
Do you have any alternative suggestions that I could try?
Thanks so much,
Laura

Sign in to comment.


Stephen23
Stephen23 on 29 Jul 2015
Edited: Stephen23 on 29 Jul 2015
The basic cause of the problem you are seeing is that the File and MaxT calulations are inside the switch case for 'RKnee', which means this code will not run for the other cases. You probably meant to do something like this:
if joint_num == 1
joint = 'LAnkle';
elseif joint_num == 2
joint = 'RAnkle';
elseif joint_num == 3
joint = 'LKnee';
elseif joint_num == 4
joint = 'RKnee';
end
File = ['TorqueData_',joint,'_Isom'];
MaxT = max (TorqueData_LAnkle_Isom);
MaxT = max (TorqueData_RAnkle_Isom);
MaxT = max (TorqueData_LKnee_Isom);
MaxT = max (TorqueData_RKnee_Isom);
But something is still fishy about this: in each loop I guess you are only loading one data file corresponding to one of the cases, so it does not make much sense for all max calculations to appear in every loop: how do you calculate max(TorqueData_RKnee_Isom) when you have only loaded the data for LAnkle?
Perhaps the entire code could be simplified to something a bit like this:
joints = {'LAnkle','RAnkle','LKnee','RKnee'};
for k = 4:-1:1
File = sprintf('TorqueData_%s_Isom',joints{k});
S = load(File);
%
MaxT(k,:) = max(S.the_appropriate_field_name);
end
Note that I used load, you can pick whatever method you are using.

Categories

Find more on Creating and Concatenating Matrices 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!