For Loop Related Question
1 view (last 30 days)
Show older comments
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
Accepted Answer
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
0 Comments
More Answers (0)
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!