This should do the trick. I've added several comments in the hope that it makes it easier for you to understand how it works. Ofcourse you won't understand everything directly when you just start out using Matlab, but I suggest to just look up the stuff you don't understand in the documentation.
clear;
aof = 4;
tableNames = table('Size',[17 1],'VariableTypes',{'string'},'VariableNames',{'paramNames'});
tableTypes = cell(1, aof); tableTypes(:) = {'double'};
t = num2cell(1:aof);
tableTypes = cell(1, aof);
tableTypes(:) = {'double'};
newTable = table('Size', [17 aof],'VariableTypes',tableTypes,'VariableNames',strcat('Subject_',cellstr(cellfun(@num2str,t(:)))));
for ii = 1:aof
fileName = ['M1-' num2str(aof) '.txt'];
if (ii == 1)
opts = detectImportOptions(fileName,'Delimiter','\n');
opts.DataLines = [26 63];
end
T = readtable(fileName,opts);
if (ii == 1)
varName = T{1:17,:};
tableNames(:,1) = varName;
end
varValue = cellfun(@str2double,T{22:38,:});
newTable(:,ii) = array2table(varValue);
end
finalTable = [tableNames newTable];
writetable(finalTable,'my_output_file.txt','Delimiter',',');
p.s. it's not my most beautiful creation but it works ;)
EDIT
The following should work with multiple files now. So let's say I have the files M1-1.txt, M1-2.txt, M2-1.txt and M2-2.txt. Now it will save to a final table of 17-by-5, where the first column consists of the parameter names, the second column is M1-1.txt data, third column M1-2.txt, fourth column M2-1.txt and fifth column M2-2.txt. Hope this was the answer you were looking for.
clear;
aof = 2;
aom = 2;
tableNames = table('Size',[17 1],'VariableTypes',{'string'},'VariableNames',{'paramNames'});
tableTypes = cell(1, aof); tableTypes(:) = {'double'};
for hh = 1:aom
t = num2cell(1:aof);
tableTypes = cell(1, aof);
tableTypes(:) = {'double'};
newTable = table('Size', [17 aof],'VariableTypes',tableTypes,'VariableNames',strcat('Subject_',num2str(hh),'_',cellstr(cellfun(@num2str,t(:)))));
for ii = 1:aof
fileName = ['M' num2str(hh) '-' num2str(ii) '.txt'];
if (hh == 1 && ii == 1)
opts = detectImportOptions(fileName,'Delimiter','\n');
opts.DataLines = [26 63];
end
T = readtable(fileName,opts);
if (hh == 1 && ii == 1)
varName = T{1:17,:};
tableNames(:,1) = varName;
end
varValue = cellfun(@str2double,T{22:38,:});
newTable(:,ii) = array2table(varValue);
end
if(hh == 1)
finalTable = [tableNames newTable];
else
finalTable = [finalTable newTable];
end
end
writetable(finalTable,'my_output_file.txt','Delimiter',',');