Help Creating For Loop to Run Functions on Multiple Files

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 Kommentare

Stephen23
Stephen23 am 20 Mär. 2025
Bearbeitet: Stephen23 am 20 Mär. 2025
"I don’t understand what “ D: ” does in their code"
D is the drive letter, usually corresponding to a hard-drive or SSD: https://en.wikipedia.org/wiki/Drive_letter_assignment
"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"
The variable FOLDER is not defined. That code will not work until it is defined or changed to another variable.
You also need to make sure that the DIR match string matches your files. Currently it matches .MAT files: do you have .MAT files?
"Is there a better way to run my function on a bunch of files?"
DIR is a good approach if you need to process files in a loop.
COPYFILE is probably a simpler approach to copying MAT files from one folder to another:
it can even copy multiple files at once.
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?
"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.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Thorsten
Thorsten am 20 Mär. 2025
Bearbeitet: Thorsten am 20 Mär. 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 Kommentare

Kristine
Kristine am 21 Mär. 2025
Bearbeitet: Kristine am 21 Mär. 2025
Than you for your response,
I want to run this function:
function [output] = do_everything(RefrenceData)
NewDateTime = Creating_Datetime(RefrenceData) ;
Table_dates = Adding_Datetime(NewDateTime, RefrenceData) ;
output = Table_dates ;
end
Instead of "disp(file)" do I write "do_everything(file)"? for context, this function is refering to two other functions I wrote. I don't these functions work on an individual file, and I want to now run it on a few hundred files.
Kristine
Kristine am 21 Mär. 2025
Bearbeitet: Kristine am 21 Mär. 2025
Also, I have a problem when I change "pwd" to my folder name "OldData" I get an error:
Unrecognized function or variable 'OldData'
In my Current Folders window in MATLAB I am in the folder than contains the folder "OldData", so why doesn't it run?
" Unrecognized function or variable 'OldData' In my Current Folders window in MATLAB I am in the folder than contains the folder "OldData", so why doesn't it run?"
Because you wrote OldData without single quotes or double quotes, thus you told MATLAB it is the name of a function/variable/script, which it dutifully tried to find or run:
mkdir('OldData')
dir(fullfile('OldData','*')) % what you should have done
. ..
dir(fullfile(OldData,'*')) % what you did
Unrecognized function or variable 'OldData'.
My code ran! Or rather it's still running, given there are 2400 files for it to process.
I'm really bad with getting syntax right. I am unsure when quotations are necessary. Is it anything that isn't a variable? And therefore are files or folders being references not considered variables?
I've sort of thrown myself into MATLAB with no coding background so I keep running into issues that are relatively simple to fix but I'm not familiar enough with any of it to realise my mistakes.
Kristine
Kristine am 21 Mär. 2025
Bearbeitet: Kristine am 21 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Environment and Settings finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2024b

Gefragt:

am 20 Mär. 2025

Bearbeitet:

am 21 Mär. 2025

Community Treasure Hunt

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

Start Hunting!

Translated by