How do I import CSV files using csvread?

10 次查看(过去 30 天)
I cannot seem to import the multiple CSV files the code I am using is below:
%% Import data
numfiles = 54; % number of CSV files mydata=cell(numfiles,1); % defining size of mydata
for i=7:length(mydata) % loop to import mutliple CSV files
myfilename = sprintf('Trial%d.csv', i); % define file name
mydata{i} = csvread(myfilename); % import files into mydata
end

采纳的回答

dpb
dpb 2014-5-10
Looks like should work presuming your filenames are ok but I'd do it slightly differently.
Try sotoo...
d=dir('*.csv'); % return the list of csv files
for i=1:length(d)
m{i}=csvread(d(i).name); % put into cell array
end
Worked here for a few in my working directory.
Note that the files need to be consistent with what content csvread can handle which is numeric array data only.
What specific problem did you encounter?
  1 个评论
Franchesca
Franchesca 2014-5-11
It still doesn't seem to work I receive the following error:
Error using dlmread (line 139) Mismatch between file and format string. Trouble reading number from file (row 1u, field 1u) ==> Devices\n
Error in csvread (line 48) m=dlmread(filename, ',', r, c);
Error in saveee (line 13) m{i}=csvread(d(i).name); % put into cell array
This is a preview of the data I am using:

请先登录,再进行评论。

更多回答(3 个)

dpb
dpb 2014-5-11
编辑:dpb 2014-5-16
As the doc says,
...All data in the input file must be numeric. dlmread does not operate on files containing nonnumeric data, even if the specified rows and columns for the read contain numeric data only.
As you can tell, csvread has the same limitation since it's a wrapper around dlmread
Also the error shows you it's the first line in the file containing the string 'Devices\n' that it barfed over.
Use textscan or textread or importdata instead...
ADDENDUM
Or, if you're adamant about using csvread, save the data w/o the header information into another file and process that one.
One additional alternative would be to read (I presume there is one) the .xls file directly w/ xlsread which will give the text into as well as the data.
ADDENDUM 2
If all you need is the array of data of the five columns, my preferred approach (being a traditionalist/minimalist in not using cell arrays where aren't needed) would be
d=dir('*.csv'); % return the list of csv files
for i=1:length(d)
m{i}=textread(d(i).name,'','headerlines',5); % put into cell array
end
The above presumes the format of each is identical in having precisely five header lines preceding the data array.
  2 个评论
BOB
BOB 2014-5-14
when I try this I get these errors:
Error using dataread Number of outputs must match the number of unskipped input fields.
Error in textread (line 175) [varargout{1:nlhs}]=dataread('file',varargin{:}); %#ok<REMFF1>
dpb
dpb 2014-5-14
Need to see the command precisely as used and a snippet of the file...

请先登录,再进行评论。


BOB
BOB 2014-5-16

dpb
dpb 2014-5-16
m{i}=textread(d(i).name,'','headerlines',5);
Gotta' have an empty format field for textread to not need multiple outputs. Looks like I typed from keyboard instead of cut 'n paste myself and missed that I missed it...
  4 个评论
dpb
dpb 2014-5-16
The error data line seems to not have the first column '1' in it for some reason...

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by