Read all files from a folder, edit them and save.

9 次查看(过去 30 天)
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')

回答(3 个)

Gopichandh Danala
Gopichandh Danala 2017-5-28
编辑:Gopichandh Danala 2017-5-28
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 个评论
Walter Roberson
Walter Roberson 2017-5-28
Stephen23
Stephen23 2017-5-28
编辑:Stephen23 2017-5-28
To anyone wanting to write good code: this answer has many "features" that should be avoided:
  1. 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.
  2. always use fullfile.
  3. 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'
  4. 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).
  5. using sprintf is preferable to strcat for making filenames.

请先登录,再进行评论。


Stephen23
Stephen23 2017-5-28
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:

vishal rawat
vishal rawat 2019-6-7
How to save file in Excel during MATLAB program executed

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by