How do I import multiple csv files?

2 次查看(过去 30 天)
Adam
Adam 2012-10-11
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 个评论
per isakson
per isakson 2012-10-11
编辑:per isakson 2012-10-11
If nothing else works TEXTSCAN will - if all data rows have identical format.
BTW: A csv-file does not have two list delimiters.

请先登录,再进行评论。

回答(1 个)

Matt Tearle
Matt Tearle 2012-10-19
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!

Translated by