MATLAB: How to create bpm file sequentially using imwrite, changing the name of the file created each time

image creationimwrite

n = 10;
M = ones (n,n,n);
for i = 1 : n
for j = 1 : n
for k = 1 : n
if i<5
M(i,j,k) = 0;
end
end
end
M_q (j,k) = M (i,j,k);
imwrite (M_q,'image_ i .bmp') % I would want to obtain a set of 10 file.bmp, each with a name that changes depending on the i index, e.g 'image_7.bmp' at the seventh iteration.
end

Best Answer

With R2017a or later you could
imwrite(M_q, "image_" + i + ".bmp")
Otherwise I suggest
filename = sprintf('image_%02d.bmp', i);
imwrite(M_q, filename)