For Loop Related Question

1 回表示 (過去 30 日間)
Jake Bowd
Jake Bowd 2020 年 7 月 14 日
回答済み: Srivardhan Gadila 2020 年 7 月 19 日
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 件のコメント
David Hill
David Hill 2020 年 7 月 14 日
Yes, can you show what you currently have?
Jake Bowd
Jake Bowd 2020 年 7 月 14 日
編集済み: Jake Bowd 2020 年 7 月 15 日
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);

サインインしてコメントする。

採用された回答

Srivardhan Gadila
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
You can refer to struct & Generate Field Names from Variables for more information.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by