Help Creating For Loop to Run Functions on Multiple Files

9 visualizaciones (últimos 30 días)
Kristine
Kristine el 20 de Mzo. de 2025
Editada: Kristine el 21 de Mzo. de 2025

I wrote a function that I want to apply to every file in a folder. I found this resource (https://www.mathworks.com/matlabcentral/answers/1445389-applying-a-single-function-to-many-files-in-one-folder) for creating a loop to apply my function to the files in a folder. The code shared is as follows:

 FolderIn  = 'D:\Your\Folder';
 FolderOut = 'D:\Your\B'
 FileList  = dir(fullfile(Folder, '*.mat’));
 for iFile = 1:numel(FileList)
   File = fullfile(Folder, FileList(iFile).name);
   Data = load(File);
   % Now do what you want with the data
   NewData = Data;
   save(fullfile(FolderOut, FileList(iFile).name), 'NewData', '-struct');
 end

1. I don’t understand what “ D: ” does in their code. In the first two lines. 2. Even when I set the directory to the folder containing the files I want to reference, the “fullfile” function does not run when I give it the file name. 3. Is there a better way to run my function on a bunch of files?

Thank you!

  3 comentarios
Kristine
Kristine el 21 de Mzo. de 2025
Thank you for your feedback.
To clarify, I don't leave the text "Folder" in my code. I tried replacing it with the folder name I wanted to access, and I know to change .mat to .csv (which is the extension of my files I want accesses).
What does DIR stand for, and what is a DIR match string in layman's terms?
Stephen23
Stephen23 el 21 de Mzo. de 2025
"What does DIR stand for..."
"... and what is a DIR match string in layman's terms?"
FileList = dir(fullfile(Folder, '*.mat’));
% ^^^^^^^^^^^^^^^^^^^^^^^^^ this
The match string (simply called "name" in the DIR documentation) has to match the filenames (possibly including paths) of the files that you wish DIR to identify.

Iniciar sesión para comentar.

Respuesta aceptada

Thorsten
Thorsten el 20 de Mzo. de 2025
Editada: Thorsten el 20 de Mzo. de 2025
The code below displays every file in the present folder. Replace
pwd
with the name of your folder if you want to process a different folder. Replace
disp
with your command that you wish to apply to each file.
folder = pwd;
filelist = dir(fullfile(folder, '*.*'));
for i = 1:numel(filelist)
file = fullfile(folder, filelist(i).name);
disp(file)
end
  6 comentarios
Kristine
Kristine el 21 de Mzo. de 2025
Editada: Kristine el 21 de Mzo. de 2025
I have gone through these, and taken the MATLAB Learning courses, but it's like learning a language where everyone tries to teach you French by speaking to you in only French. Some people learn by being immersed, but I don't. I just need a good translation book that defines everything.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Productos


Versión

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by