1

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?

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Prasanth
  • 11
  • 1
  • 5

1 Answers1

2

The output from dir will 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:

files = dir('foldername');
files = files(~[files.isdir]);

Or, as Vahe Tshitoyan suggests, you can collect only the .csv files like so:

files = dir('foldername\*.csv');

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 xlsread it 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 by dir and pass that along with the file name to fullfile, like so:

for i = 1:N
  thisfile = xlsread(fullfile(files(i).folder, files(i).name));
  % Subsequent processing
end
gnovice
  • 125,304
  • 15
  • 256
  • 359
  • I am getting a invoke error while trying this. This code is `xlsread(fullfile(files(i).folder, files(i).name))`attempting to read the files as .xlsx from my folder. But my folder has .csv files – Prasanth Jun 20 '17 at 16:16
  • @Prasanth: What is an "invoke" error? What do you see in `files(i).folder` and `files(i).name`? – gnovice Jun 20 '17 at 16:19
  • So it says C:\Users\Documents\MATLAB\myfolder\files.xlsx couldn't be found. `files(i).folder` displays the path to myfolder and `files(i).name` displays a dot '.' – Prasanth Jun 20 '17 at 16:25
  • 2
    use `dir('foldername\*.csv')`, and maybe `files{i}` instead of `files(i)`? – Vahe Tshitoyan Jun 20 '17 at 16:28
  • 1
    @VaheTshitoyan: `files` is a structure array, so `files(i)` is correct. – gnovice Jun 20 '17 at 16:43
  • @gnovice Yes thanks, I wrote that then realised the mistake. I usually do `filenames = {files.name}`, then use `filenames{i}`, hence the confusion. – Vahe Tshitoyan Jun 20 '17 at 16:47
  • @gnovice Yes the edited version of your code reads the files without any errors. But for some wierd reason it omits the first and last column of my files and reads the rest. Also all the text/character fields are displayed as NaN. The headers are not read as well – Prasanth Jun 20 '17 at 17:19
  • @Prasanth: Take a look at the docs for [`xlsread`](https://www.mathworks.com/help/matlab/ref/xlsread.html). The first output is just numeric data (with `nan` placeholders), the second output is text data, and the third output is both (i.e. "raw" data). – gnovice Jun 20 '17 at 17:25
  • @gnovice how can I obtain the 'raw' data as such as it is in each file? – Prasanth Jun 20 '17 at 17:36
  • @Prasanth: Get the third output from `xlsread` like this: `[~, ~, rawData] = xlsread(...` – gnovice Jun 20 '17 at 17:37
  • @gnovice Perfect ! Thank you – Prasanth Jun 20 '17 at 17:41