Clear Filters
Clear Filters

how to use save a new file with a prefix before the original file name in a loop

8 views (last 30 days)
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

Accepted Answer

Image Analyst
Image Analyst on 25 Sep 2018
Edited: Image Analyst on 25 Sep 2018
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

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!