I have a folder with many .csv files. I need to load one file at at time and perform some operations. I tried loading files in a loop using the csvread and xlsread command but it does not work. My files are in a .csv format but csvread does not read it as my files have text and numbers. Hence I used `xlsread' but I get error saying the file.csv could not be found. I am unsure what the problem is.I use the following code to read files in a loop:
files=dir('foldername');
N=length(files);
for i=1:N
thisfile=xlsread(files(i).name);
end
Variable files is read as a structure and it displays filename, filelocation,bytes and dates. Should I convert to an array in order to read the contents of the file?
The output from
dirwill include the entries.and.., along with any other subfolders in'foldername', so you will want to remove those first. You can do that like so:Or, as Vahe Tshitoyan suggests, you can collect only the .csv files like so:
If you have any other files you don't want to process in the folder, you should definitely use the second approach.
If you're getting an error saying the file cannot be found in your call to
xlsreadit probably means you need to specify the full path to the file instead of just the file name. You can use the'folder'field of the structure returned bydirand pass that along with the file name tofullfile, like so: