MATLAB: How to save save split images in multiple filenames

filenamesimagesmultiple

I have a code which shows the multiple images of a scanned *.tif document.
for i=1:5
d=imshow(imread('gi2.tif',i));
figure;
end
I need to save these images as multiple files. I used the following code for that:
basename='im_'
for i=1:5
d=imshow(imread('gi2.tif',i));
figure;
filename=[basename,num2str(i)];
end
But the code is just creating a variable filename with data im_5. I also tried using "sprintf" using the following code:
for i=1:5
d=imread('gi2.tif',i));
filename=sprintf('im_%d.jpg',i)
end
But this basically prints the filename im_<no.> and nothing else.
Please support!!

Best Answer

That solves your problem?
for i=1:5
imwrite(imread('gi2.tif',i),['im_' num2str(i) '.tif']);
end