For Loop Related Question
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Srivardhan Gadila
2020-7-19
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!