For Loop Related Question

1 view (last 30 days)
Jake Bowd
Jake Bowd on 14 Jul 2020
Answered: Srivardhan Gadila on 19 Jul 2020
Hi
So I have written a script, which essentially repeats code 3 times (once for group 'A', once for group 'B' and once for group 'C'). Within each group, there are n number of individual data, i.e. I load in 25 peoples data for Group A, 26 peoples data for Group B and 23 peoples data for Group C.
I have created a for loop which finds averages for each group.
However, my question is: Is there an easy way to put another for loop in in order for the script to run so that each of the three groups can be inputted one after each other?
Feels like a very simple thing to do but cannot visualise how to do it.
Many thanks
Jake
  2 Comments
David Hill
David Hill on 14 Jul 2020
Yes, can you show what you currently have?
Jake Bowd
Jake Bowd on 14 Jul 2020
Edited: Jake Bowd on 15 Jul 2020
Hi David,
I'll reduce the code significantly to just a couple of variables to show the example. Please note my two groups are (Pre_HTO and Post_HTO):
%% PRE HTO GROUP DATA
cd('C:\Users\c1734806\OneDrive - Cardiff University\PhD Folder\Thesis\NL_Simulations_Complete\Pre_HTO);
files = dir ('*_Contact_Forces.mat' ); % finds all of the files in the cd with that in it's name
for ii=1:length(files); % calculates how many files there with above name
ALL_DATA{ii} = load( files(ii).name);% loads in the data with said filename
x {(ii)}= ALL_DATA{1, ii}.Results.CFMedialAverage.Res';
end
Pre_HTO_DATA = ALL_DATA;
Pre_HTO_files = files;
Pre_HTO_CFMedial_Group = cell2mat( x );
Pre_HTO_CFMedial_Group_Average = mean(Pre_HTO_CFMedial_Group,2);
%% POST HTO GROUP DATA
cd('C:\Users\c1734806\OneDrive - Cardiff University\PhDFolder\Thesis\NL_Simulations_Complete\Post_HTO);
files = dir ('*_Contact_Forces.mat' ); % finds all of the files in the cd with that in it's name
for ii=1:length(files); % calculates how many files there with above name
ALL_DATA{ii} = load( files(ii).name);% loads in the data with said filename
x {(ii)}= ALL_DATA{1, ii}.Results.CFMedialAverage.Res';
end
Post_HTO_DATA = ALL_DATA;
Post_HTO_files = files;
Post_HTO_CFMedial_Group = cell2mat( x );
Post_HTO_CFMedial_Group_Average = mean(Post_HTO_CFMedial_Group,2);

Sign in to comment.

Accepted Answer

Srivardhan Gadila
Srivardhan Gadila on 19 Jul 2020
Based on the above information, one possible way is to make use of struct as follows:
groupNames = ["Pre_HTO", "Post_HTO"];
folderPath = "C:\Users\c1734806\OneDrive - Cardiff University\PhD Folder\Thesis\NL_Simulations_Complete\";
for i = 1:numel(groupNames)
groupData.(groupNames(i)) = myGroupFunc(folderPath,groupNames(i));
end
groupData
function groupStruct = myGroupFunc(folderPath,group)
cd(folderPath+group)
files = dir('*_Contact_Forces.mat'); % finds all of the files in the cd with that in it's name
for ii=1:length(files) % calculates how many files there with above name
ALL_DATA{ii} = load(files(ii).name);% loads in the data with said filename
x{(ii)} = ALL_DATA{1, ii}.Results.CFMedialAverage.Res';
end
groupStruct.DATA = ALL_DATA;
groupStruct.files = files;
groupStruct.CFMedial_Group = cell2mat( x );
groupStruct.CFMedial_Group_Average = mean(Pre_HTO_CFMedial_Group,2);
end
You can refer to struct & Generate Field Names from Variables for more information.

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!