How do I import multiple csv files?

1 view (last 30 days)
Adam
Adam on 11 Oct 2012
Hi,
I have multiple csv files to import. To start with I followed the Matlab example for importing multiple text files, which is found in the documentation. However I have an error message that pops up:
Error in importfile (line 17)
delimiter = {',',' '};
Output argument "data" (and maybe others) not assigned during call to "filepath"
The csv files are named from S500D.csv to S593D.csv but there are only 82 files (not the 93 you might expect) which means that the file names are not necessarily in sequence. The files contain date and time data as well as numerical and require comma and space delimitation.
How can I import these files into Matlab into a cell array as in the Matlab example?
Thanks
Adam
  1 Comment
per isakson
per isakson on 11 Oct 2012
Edited: per isakson on 11 Oct 2012
If nothing else works TEXTSCAN will - if all data rows have identical format.
BTW: A csv-file does not have two list delimiters.

Sign in to comment.

Answers (1)

Matt Tearle
Matt Tearle on 19 Oct 2012
If the files all have the same format, and you have access to a recent version of MATLAB (12b would be nice...), use the Import Tool to do it interactively, then generate the code. Then apply the generated function to all the files.
You can use ls or dir to get an array of file names, then loop through that:
fnm = cellstr(ls('*.csv'));
for k = 1:length(fnm)
x = myimportfunction(fnm{k});
% do stuff with x
end
If you don't have the shiny new Import Tool, you could (a) upgrade :) or (b) use csvread or textscan, but you'd need to show the format of your file for us to be able to give much further guidance.

Community Treasure Hunt

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

Start Hunting!