简体   繁体   中英

csvread drops rows in MATLAB

I create some random data in 5 columns and store that as a CSV file. This works fine, my CSV actually contains 1001 rows; my data with the header (I checked this by opening it in my spreadsheet editor). However, when I read it in MATLAB again using csvread , I get an error if I do not specify the delimiter and when I do specify the delimiter data contains only 957 rows, thus the header and first 42 rows are missing. What is going on here?

code:

A = rand(1e3,5);
out = fopen('output.csv','w');
fprintf(out,['ColumnA', ',', 'ColumnB', ',', 'ColumnC',  ',', 'ColumnD',  ',', 'ColumnE','\n']);
fclose(out);
dlmwrite('output.csv', A, 'delimiter',',','-append');
data = csvread('output.csv',',');

Error:

Error using dlmread (line 139) Mismatch between file and format string. Trouble reading number from file (row 1u, field 1u) ==> ColumnA,ColumnB,ColumnC,ColumnD,ColumnE\\n

Error in csvread (line 48) m=dlmread(filename, ',', r, c);

Am I missing something stupid in writing or reading the file?

I am running MATLAB R2012a on a 64bit Windows 7 machine. (Though I hope to upgrade to R2015b in the coming month)

The reason is that you are using invalid csvread syntax. From help csvread :

M = csvread('FILENAME') reads a comma separated value formatted file FILENAME. The result is returned in M. The file can only contain numeric values.

M = csvread('FILENAME',R,C) reads data from the comma separated value formatted file starting at row R and column C. R and C are zero- based so that R=0 and C=0 specifies the first value in the file.

M = csvread('FILENAME',R,C,RNG) reads only the range specified by RNG = [R1 C1 R2 C2] where (R1,C1) is the upper-left corner of the data to be read and (R2,C2) is the lower-right corner. RNG can also be specified using spreadsheet notation as in RNG = 'A1..B7'.

You cannot tell csvread what delimiter to use. When you call it that way, it interprets ' as a numeric 44 (in ascii) and uses that as starting line.

Call this instead: data = csvread('output.csv',1);

First, on MATLAB 2014b, I have a different error

Error using dlmread (line 138) HeaderLines must be integer-valued. Error in csvread (line 47) m=dlmread(filename, ',', r, c);

In MATLAB, I usually import data through its data import interface. On Home tab, click on Import Data , there you have many options to make sure data import is correct. The best part of the interface is the code generation function. You have the option to generate code that MATLAB actually uses to import data. For example, this is what I got :

filename = 'output.csv';
delimiter = ',';
startRow = 2;
formatSpec = '%f%f%f%f%f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'HeaderLines', startRow-1, 'ReturnOnError', false);
fclose(fileID);
ColumnA = dataArray{:, 1};
ColumnB = dataArray{:, 2};
ColumnC = dataArray{:, 3};
ColumnD = dataArray{:, 4};
ColumnE = dataArray{:, 5};
clearvars filename delimiter startRow formatSpec fileID dataArray ans;

It works correctly. As you can see, you can easily modify the code to tailor the import function to your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM