MATLAB: How to concentrate matrix 100 times

anti-patterncomplexconcentrate arraydynamic variable namesevil evalMATLABslow

for testnum=1:100
filesavepath=['H:\Desktop\0208\' num2str(testnum) '_sen'];
load(fullfile([filesavepath '\0208_' num2str(testnum) '_sen_avg_3.mat']),'matrix_avg');
eval(sprintf('matrix_%g=matrix_avg;',testnum));
end
matrix=cat(2,matrix_1,matrix_2,matrix_3,matrix_4,matrix_5,matrix_6,matrix_7,matrix_8,matrix_9,matrix_10,matrix_11,matrix_12);
I am trying to concentrate 100 matrix for instance. I know how to load them 100 times at different folder, but how to make my last line
matrix=cat(2,matrix_1,matrix_2,…..matrix_99,matrix_100);
Thank you!

Best Answer

Your approach is entirely in the wrong direction: using numbered variables is a sign that you are doing something wrong with your data/code design. Read this to know some of the reasons why:
The simpler alternative is to load the file data into an output variable (which is a structure), then your task is trivially easy (and much more efficient):
N = 100;
C = cell(1,N);
for k = 1:N
F = fullfile(...);
S = load(F,'matrix_avg');
C{k} = S.matrix_avg;
end
M = cat(2,C{:})
See also: