MATLAB: How to use save a new file with a prefix before the original file name in a loop

codefile nameImage Processing Toolboxloop

Hi,
I would like to save each file in a loop using a prefix (in this case, "blur_") before the name of the original file. This is the code I have so far
for i = 1:N
blur{i} = imgaussfilt(imdata{i},10)
imshow(blur{1})
imwrite(blur{i},sprintf('blurred_circle_%d.png',i))
end
using this code I am able to define a prefix (blurred_circle_) and then to give a number to each file and save them. the point is that I need my output to be like 'blurred_(original filename).png'. How can I do that?
thanks A

Best Answer

Try this:
files = dir('*.png');
for k = 1 : length(files)
thisName = files(k).name
fullFileName = fullfile(files(k).folder, files(k).name)
theImage = imread(fullFileName);
subplot(1, 2, 1);
imshow(theImage);
drawnow;
blurredImage = imgaussfilt(theImage, 10);
subplot(1, 2, 2);
imshow(blurredImage);
drawnow;
outputBaseFileName = sprintf('Blur_%s', thisName);
outputFullFileName = fullfile(files(k).folder, outputBaseFileName)
imwrite(blurredImage, outputFullFileName);
end
Related Question