MATLAB: Saving images after binarizing them

binzarizeimageimage processingimwriteMATLABMATLAB and Simulink Student Suite

Hi all. I have this code:
Namelist = cell(length(filelist), 1);
invariants = zeros(length(filelist), 11); % change the number at the end to
% number of columns % 11 is 4th order. 6 is 3rd
for i = 1:length(filelist)
imagename = filelist(i).name;
Namelist{i, 1} = filelist(i).name;
I = imread ([path, '/', imagename]);
I = im2double (I);
I = 1-I;
I = imbinarize(I);
Due to issues with the end result, when I turn my image into 0s and 1s, I want to be able to save all of them into a separate folder so I can see what changing the threshold does to them. Is there a way to automatically save all the photos using imwrite() to an external file, but keeping the file names they have?
Example:
Original image name: 'Letter 9', 'Letter g', 'Letter x'
after binarize I want to save them again using the same name.
Thank you!

Best Answer

Don't use path as a variable name - it's the name of a built-in function.
Just specify a different output folder, like "Binary images":
outputFolder = fullfile(filelist(i).folder, 'Binary Images');
outputFileName = fullfile(outputFolder, imagename);
imwrite(I, outputFileName);