Loop through files in a folder in matlab
Solution 1
Looping through all the files in the folder is relatively easy:
files = dir('*.csv');
for file = files'
csv = load(file.name);
% Do some stuff
end
Solution 2
At first, you must specify your path, the path that your *.csv
files are in there
path = 'f:\project\dataset'
You can change it based on your system.
then,
use dir
function :
files = dir (strcat(path,'\*.csv'))
L = length (files);
for i=1:L
image{i}=csvread(strcat(path,'\',file(i).name));
% process the image in here
end
pwd
also can be used.
Fantastic Mr Fox
I am a software engineer and roboticist. Building robots is my thing!
Updated on July 08, 2022Comments
-
Fantastic Mr Fox almost 2 years
I have a set of days of log files that I need to parse and look at in matlab.
The log files look like this:
LOG_20120509_120002_002.csv (year)(month)(day)_(hour)(minute)(second)_(log part number)
The logs increment hourly, but sometimes the seconds are one or two seconds off (per hour) which means i need to ignore what they say to do
loadcsv
.I also have another file:
LOG_DATA_20120509_120002.csv
which contains data for the whole hour (different data).
The overall objective is to:
loop through each day loop through each hour read in LOG_DATA for whole hour loop through each segment read in LOG for each segment compile a table of all the data
I guess the question is then, how do i ignore the minutes of the day if they are different? I suspect it will be by looping through all the files in the folder, in which case how do i do that?
-
Fantastic Mr Fox almost 12 yearsI had a feeling it would be something with dir, i couldn't quite put my finger on it though. Thanks for your response.
-
Juan Sebastian Totero about 11 yearsmmm this is not working for me... file = files simply copies files into file :(
-
Isaac about 11 yearsTry
file = files'
; it may require thatfiles
is a row-array. -
John Bensin almost 11 years
-
Fantastic Mr Fox almost 10 years@JohnBensin I know this is late but that is not entirely true.
load
can get space separated data out of files and other specific things. -
Sibbs Gambling almost 10 yearsWorks good. However, one potential pitfall: pay attention to the file order as a result of the file names, if the file order matters. I spent hours debugging and realized this. [facepalm]
-
vigamage over 8 yearsWhen I do this, I get an error saying
Unable to read file 'abcdef.csv' : ': no such file or directory.
why is that? -
Isaac over 8 years@vigamage, are you targeting the current directory? Or are you using
dir('some/path/*.csv')
? -
Charlie Parker over 8 yearswhy do you need the
'
(transpose)? -
Isaac over 8 years@CharlieParker, because matlab for-loops treat row-vectors and column-vectors differently (or at least they did when I wrote this).
-
Charlie Parker about 6 yearshow do you actually deal with this without having to cd to a specific directory? I have a directory with more directories with those directory having files. It seems really dum to have to cd to directories then call
dir
, is there not a better solution? -
Isaac about 6 years@CharlieParker, you can call
dir
directly on a subdirectory to get the contents of that subdirectory. You can also loop through the names returned bydir
, and callisdir
on each one to figure out if they are directories themselves. In this way you can recursively list all the files in a nested directory. -
Cris Luengo almost 6 yearsInstead of
strcat
, usefullfile
:dir(fullfile(path,'*.csv'))
. See documentation: mathworks.com/help/matlab/ref/fullfile.html