Read all files from a folder, edit them and save.
7 views (last 30 days)
Show older comments
I have written a code to read image from a folder, edit it's array and save it. How can I do the same for multiple files at once ? All files are named as 'TangoBlack_00000' with the number increasing by 1 for each file. So I have files numbered till 'TangoBlack_00110'. Also can I edit the files directly instead of saving them with different file names ? I have attched the code below:
a=imread('TangoBlack_00009.bmp');
a(33:40,:)=0;
a(153:160,:)=0;
imwrite(a,'TB_09.bmp')
0 Comments
Answers (3)
Gopichandh Danala
on 28 May 2017
Edited: Gopichandh Danala
on 28 May 2017
Give the actual file(folderPath1) and write file(WriteDir) folder paths into the code and you should be able to modify and save them to the desired folder with the name you want.
% folderPath1 = actualFile folder
folderPath1 = 'D:\MATLAB\actualFilesFolder';
cd(folderPath1); % path of the folder
% WriteDir = WriteFile Folder
WriteDir = 'D:\MATLAB\WriteFolder';
files1 = dir('**');
files1(1:2) = [];
totalFiles = numel(files1);
for i =1:totalFiles
Fileaddress{i,1}=strcat(folderPath1,'\',files1(i).name);
file{i} = imread(Fileaddress{i,1});
% Edit the file
file{i}(33:40,:)=0;
file{i}(153:160,:)=0;
cd(WriteDir) % go to dir where you want to save updated files
writeFileName = strcat('TB_0',num2str(i),'.bmp');
imwrite(file{i},writeFileName)
cd(folderPath1) % return to actualFile folder
end
% This is assuming you are using a Windows computer, if you have a Mac the folders convention will be slightly different
2 Comments
Walter Roberson
on 28 May 2017
- Please get into the habit of using fullfile() instead of strcat of directory separators.
- your code removes the first two entries from the returned list, probably to try to get rid of "." and ".." . That is not correct coding. See https://www.mathworks.com/matlabcentral/answers/338973-how-to-solve-with-this-error-subscript-indices-must-either-be-real-positive-integers-or-logicals#comment_451762
- if you were to use writeFileName = fullfile(Writedir, strcat('TB_0',num2str(i),'.bmp')) then you would not need to cd(Writedir) or cd(folderPath1)
- Instead of that strcat to construct the name, you should use writeFileName = fullfile(Writedir, sprintf'TB_%05d.bmp', i)) as otherwise you will not get the correct number of digits as i changes
Stephen23
on 28 May 2017
Edited: Stephen23
on 28 May 2017
To anyone wanting to write good code: this answer has many "features" that should be avoided:
- do NOT use cd for this task! Using cd is slow, and makes debugging much harder. Much faster and simpler is to always use absolute/relative file paths.
- always use fullfile.
- if you are looking for particular file-type then matching all files and folders using the dir match string '**' is a really bad idea. Much better is to match only the required file types, e.g.: '*.jpg'
- The badly-named variable file actually stores the loaded image data, but is not preallocated and gets enlarged on each loop iteration. Better: cell array preallocation, or simply not storing every array if it in not required to have them all in memory (like in this answer, where each image gets processed independently of the others).
- using sprintf is preferable to strcat for making filenames.
Stephen23
on 28 May 2017
How to read multiple files is explained extensively in the documentation, on this forum, and in the wiki:
etc
The first thing to decide is if you want to generate the file names, or if you want to read the names of existing files:
- generate names: use sprintf and fullfile.
- read names: use dir and fullfile.
You can also find some examples with my FEX submission natsortfiles:
0 Comments
See Also
Categories
Find more on Environment and Settings 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!