Can my code be written in a more efficient way?
1 次查看(过去 30 天)
显示 更早的评论
Hi all,
I have written a code that aims to collate mutiple mat files in one mat files per trial (15 trials). See below. Can this be written in a more efficient way?
% Specify participant folder
DotFolder = [Folder '\Processed data\Xsens Dot_Eular']; %Folder with all Dot files in Eular
% Get files names
all_Dotfiles = dir(fullfile(DotFolder, '*.mat'));
% Get files names per trial
Trial_1 = all_Dotfiles (1:5);
Trial_2 = all_Dotfiles (6:10);
Trial_3 = all_Dotfiles (11:15);
Trial_4 = all_Dotfiles (16:20);
Trial_5 = all_Dotfiles (21:25);
Trial_6 = all_Dotfiles (26:30);
Trial_7 = all_Dotfiles (31:35);
Trial_8 = all_Dotfiles (36:40);
Trial_9 = all_Dotfiles (41:45);
Trial_10 = all_Dotfiles (46:50);
Trial_11 = all_Dotfiles (51:55);
Trial_12 = all_Dotfiles (56:60);
Trial_13 = all_Dotfiles (61:65);
Trial_14 = all_Dotfiles (66:70);
Trial_15 = all_Dotfiles (71:75);
% Make results folder
if ~isfolder([DotFolder '_Collated'])
mkdir([DotFolder '_Collated'])
end
% Collate each file per trial and save collated files
T1 = {Trial_1.name};
for k = numel(T1):-1:1
Data(k) = load(fullfile(DotFolder,T1{k}));
save(fullfile([DotFolder '_Collated'],[Trial_1(k).name(1:end-15) '.mat']), 'Data');
end
clearvars -except Participant Folder DotFolder all_Dotfiles Trial_2 Trial_3 Trial_4 Trial_5 Trial_6 Trial_7 Trial_8 Trial_9 Trial_10 ...,
Trial_11 Trial_12 Trial_13 Trial_14 Trial_15
T2 = {Trial_2.name};
for k = numel(T2):-1:1
Data(k) = load(fullfile(DotFolder,T2{k}));
save(fullfile([DotFolder '_Collated'],[Trial_2(k).name(1:end-15) '.mat']), 'Data');
end
clearvars -except Participant Folder DotFolder all_Dotfiles Trial_3 Trial_4 Trial_5 Trial_6 Trial_7 Trial_8 Trial_9 Trial_10 ...,
Trial_11 Trial_12 Trial_13 Trial_14 Trial_15
T3 = {Trial_3.name};
for k = numel(T3):-1:1
Data(k) = load(fullfile(DotFolder,T3{k}));
save(fullfile([DotFolder '_Collated'],[Trial_3(k).name(1:end-15) '.mat']), 'Data');
end
clearvars -except Participant Folder DotFolder all_Dotfiles Trial_4 Trial_5 Trial_6 Trial_7 Trial_8 Trial_9 Trial_10 ...,
Trial_11 Trial_12 Trial_13 Trial_14 Trial_15
T4 = {Trial_4.name};
for k = numel(T4):-1:1
Data(k) = load(fullfile(DotFolder,T4{k}));
save(fullfile([DotFolder '_Collated'],[Trial_4(k).name(1:end-15) '.mat']), 'Data');
end
clearvars -except Participant Folder DotFolder all_Dotfiles Trial_5 Trial_6 Trial_7 Trial_8 Trial_9 Trial_10 ...,
Trial_11 Trial_12 Trial_13 Trial_14 Trial_15
T5 = {Trial_5.name};
for k = numel(T5):-1:1
Data(k) = load(fullfile(DotFolder,T5{k}));
save(fullfile([DotFolder '_Collated'],[Trial_5(k).name(1:end-15) '.mat']), 'Data');
end
clearvars -except Participant Folder DotFolder all_Dotfiles Trial_6 Trial_7 Trial_8 Trial_9 Trial_10 ...,
Trial_11 Trial_12 Trial_13 Trial_14 Trial_15
T6 = {Trial_6.name};
for k = numel(T6):-1:1
Data(k) = load(fullfile(DotFolder,T6{k}));
save(fullfile([DotFolder '_Collated'],[Trial_6(k).name(1:end-15) '.mat']), 'Data');
end
clearvars -except Participant Folder DotFolder all_Dotfiles Trial_7 Trial_8 Trial_9 Trial_10 ...,
Trial_11 Trial_12 Trial_13 Trial_14 Trial_15
T7 = {Trial_7.name};
for k = numel(T7):-1:1
Data(k) = load(fullfile(DotFolder,T7{k}));
save(fullfile([DotFolder '_Collated'],[Trial_7(k).name(1:end-15) '.mat']), 'Data');
end
clearvars -except Participant Folder DotFolder all_Dotfiles Trial_8 Trial_9 Trial_10 ...,
Trial_11 Trial_12 Trial_13 Trial_14 Trial_15
T8 = {Trial_8.name};
for k = numel(T8):-1:1
Data(k) = load(fullfile(DotFolder,T8{k}));
save(fullfile([DotFolder '_Collated'],[Trial_8(k).name(1:end-15) '.mat']), 'Data');
end
clearvars -except Participant Folder DotFolder all_Dotfiles Trial_9 Trial_10 ....,
Trial_11 Trial_12 Trial_13 Trial_14 Trial_15
T9 = {Trial_9.name};
for k = numel(T9):-1:1
Data(k) = load(fullfile(DotFolder,T9{k}));
save(fullfile([DotFolder '_Collated'],[Trial_9(k).name(1:end-15) '.mat']), 'Data');
end
clearvars -except Participant Folder DotFolder all_Dotfiles Trial_10 Trial_11 Trial_12 Trial_13 Trial_14 Trial_15
T10 = {Trial_10.name};
for k = numel(T10):-1:1
Data(k) = load(fullfile(DotFolder,T10{k}));
save(fullfile([DotFolder '_Collated'],[Trial_10(k).name(1:end-15) '.mat']), 'Data');
end
clearvars -except Participant Folder DotFolder all_Dotfiles Trial_11 Trial_12 Trial_13 Trial_14 Trial_15
T11 = {Trial_11.name};
for k = numel(T11):-1:1
Data(k) = load(fullfile(DotFolder,T11{k}));
save(fullfile([DotFolder '_Collated'],[Trial_11(k).name(1:end-15) '.mat']), 'Data');
end
clearvars -except Participant Folder DotFolder all_Dotfiles Trial_12 Trial_13 Trial_14 Trial_15
T12 = {Trial_12.name};
for k = numel(T12):-1:1
Data(k) = load(fullfile(DotFolder,T12{k}));
save(fullfile([DotFolder '_Collated'],[Trial_12(k).name(1:end-15) '.mat']), 'Data');
end
clearvars -except Participant Folder DotFolder all_Dotfiles Trial_13 Trial_14 Trial_15
T13 = {Trial_13.name};
for k = numel(T13):-1:1
Data(k) = load(fullfile(DotFolder,T13{k}));
save(fullfile([DotFolder '_Collated'],[Trial_13(k).name(1:end-15) '.mat']), 'Data');
end
clearvars -except Participant Folder DotFolder all_Dotfiles Trial_14 Trial_15
T14 = {Trial_14.name};
for k = numel(T14):-1:1
Data(k) = load(fullfile(DotFolder,T14{k}));
save(fullfile([DotFolder '_Collated'],[Trial_14(k).name(1:end-15) '.mat']), 'Data');
end
clearvars -except Participant Folder DotFolder all_Dotfiles Trial_15
T15 = {Trial_15.name};
for k = numel(T15):-1:1
Data(k) = load(fullfile(DotFolder,T15{k}));
save(fullfile([DotFolder '_Collated'],[Trial_15(k).name(1:end-15) '.mat']), 'Data');
end
0 个评论
采纳的回答
Abolfazl Chaman Motlagh
2022-2-28
if clearvars are not important, this is exactly equal to what you're doing:
% Specify participant folder
DotFolder = [Folder '\Processed data\Xsens Dot_Eular']; %Folder with all Dot files in Eular
% Get files names
all_Dotfiles = dir(fullfile(DotFolder, '*.mat'));
% Get files names per trial
for i=1:15
Trial{i} = all_Dotfiles((i-1)*5+1:i*5);
end
% Make results folder
if ~isfolder([DotFolder '_Collated'])
mkdir([DotFolder '_Collated'])
end
% Collate each file per trial and save collated files
for i=1:15
T = {Trial{i}.name};
Trial_loc = Trial{i};
for k = numel(T):-1:1
Data(k) = load(fullfile(DotFolder,T{k}));
save(fullfile([DotFolder '_Collated'],[Trial_loc(k).name(1:end-15) '.mat']), 'Data');
end
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Search Path 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!