MATLAB: Threshold images and save as jpeg in loop

fileimage processingMATLABsavethreshold

Hi, I would like to run a threshold to approx 50 images of mine and then save the output as jpg, currently im not managing as the saved file says it cannot be read. below is my code
for i = 1:numel(I)
%Obtain red matrix of image
filename = fullfile(path, I(i).name);
im = imread(filename);
%Extract Red Channel
b = im(:,:,1);
%Normalising red values
Norm=double(b(:))./255;
%Reshaping to fit image shape
[f,g]=size(b);
imn=reshape(Norm,[f,g]);
%threshold image
thresh = imn .* imn>(0.6258);
thresh_im = uint8(thresh);
name = int2str(i);
save([name, '.jpg'], 'thresh_im')
end
Please help?

Best Answer

Use imwrite instead of save.
for i = 1:numel(I)
% Obtain red matrix of image
filename = fullfile(path, I(i).name);
im = imread(filename);
% Extract Red Channel
b = im(:, :, 1);
% Normalising red values
Norm = double(b(:)) ./ 255;
% Reshaping to fit image shape
[f, g] = size(b);
imn = imresize(Norm, [f, g]);
% Threshold image
thresh = imn .* imn>(0.6258);
thresh_im = uint8(thresh);
% Save the image
imwrite(thresh_im, ['img', num2str(i), '.jpg']);
end