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

25 visualizaciones (últimos 30 días)
sanchit ingale
sanchit ingale el 27 de Mayo de 2017
Respondida: vishal rawat el 7 de Jun. de 2019
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')

Respuestas (3)

Gopichandh Danala
Gopichandh Danala el 28 de Mayo de 2017
Editada: Gopichandh Danala el 28 de Mayo de 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 comentarios
Walter Roberson
Walter Roberson el 28 de Mayo de 2017
Stephen23
Stephen23 el 28 de Mayo de 2017
Editada: Stephen23 el 28 de Mayo de 2017
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.

Iniciar sesión para comentar.


Stephen23
Stephen23 el 28 de Mayo de 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:

vishal rawat
vishal rawat el 7 de Jun. de 2019
How to save file in Excel during MATLAB program executed

Categorías

Más información sobre Environment and Settings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by