For Loop Related Question
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Jake Bowd
il 14 Lug 2020
Risposto: Srivardhan Gadila
il 19 Lug 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 Commenti
Risposta accettata
Srivardhan Gadila
il 19 Lug 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 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!