MATLAB: How to save every iteration into workspace

it is saving just the last iteratonsave for loop iterations into work spacesave into workspace

my code would save just the last iteration.
for c=1:170
name1 = strcat(folder_name1,'/',filesStruct1(c).name);
alpha = (((double(dicomread(name1)).*pi)./(4096)) - (pi./2))
dicomwrite(alpha, (strcat('Results/',filesStruct1(c).name)));
save(mfilename)
end
can you please edit my code so it saves alpha 170 times with 170 different 256*240 matrices thanks in advance

Best Answer

Use fullfile rather than string concatenation. To store the imported images in a loop simply preallocate a cell array before the loop, and then use indexing:
N = 170;
C = cell(1,N);
for k = 1:N
name = fullfile(folder_name1,filesStruct1(c).name);
alpha = double(dicomread(name1))*pi/4096 - pi/2
dicomwrite(alpha, fullfile('Results',filesStruct1(c).name));
C{k} = alpha;
end
save(mfilename,'C')