I want to do the same action to every files(.png) in one folder, but I face some problem.

1 view (last 30 days)
I want to put 1092 files(109 x 109 .png image) in the center of 227 x 227 matrix, so I write down a code like this
files = dir('*.png') ; % I am in folder of the png files
N = length(files) ; % total number of files
% loop for each file
for i = 1:N
x = files(i).name ;
b=imread('x');
a=zeros(227);
a(60:168,69:159)=b;
a=uint8(a);
end
end
answer
Error using imread>get_full_filename (line 516)
File "x" does not exist.
Error in imread (line 340)
fullname = get_full_filename(filename);
How can I process all my files in the folder, and save as .png in a new folder ?

Accepted Answer

Image Analyst
Image Analyst on 19 Oct 2019
Try the padarray() function.
Or if you want to do it by indexing:
baseFileName = files(i).name ;
b=imread(baseFileName);
a=zeros(227, 227, 'uint8'); % Add class and get rid of a=uint8(a); line of code
  1 Comment
YuCheng Chen
YuCheng Chen on 21 Oct 2019
b=imread(baseFileName) has solved.
I have the 227x227(uint8) matrix called "a" now
How do I save "a" as png(and name it) in a new folder of my computer?

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!