MATLAB: Problem with reading and writing image

Image Processing Toolboxreading and saving images

NumberOfimages=4; %chose the number of images you want to give input
prefix_image='data ('; %change the desired input image name here only
fileformat=').tif'; %change the desired input image format here only
for num=1:NumberOfimages
I = imread(strcat(prefix_image,num2str(num),fileformat));
fileName = sprintf('C:\\result\\%02d',num);
imwrite (I, 'fileName', 'jpg');
end
i tried this code in order to read and save images,although the reading part worked well,the saving part doesn't work,can any one plz help me? my images are: data (1),data (2),…

Best Answer

It's
imwrite (I, fileName, 'jpg');
or
imwrite (I, [fileName '.jpg']);
or
fileName = sprintf('C:\\result\\%02d.jpg',num);
imwrite (I, fileName);