简体   繁体   English

csvread在MATLAB中删除行

[英]csvread drops rows in MATLAB

I create some random data in 5 columns and store that as a CSV file. 我在5列中创建了一些随机数据,并将其存储为CSV文件。 This works fine, my CSV actually contains 1001 rows; 这个工作正常,我的CSV实际上包含1001行; 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. 但是,当我再次使用csvread在MATLAB中读取它时,如果我未指定定界符,并且当我指定定界符data仅包含957行时出现错误,因此标题和前42行丢失。 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. 使用dlmread时出错(第139行)文件和格式字符串不匹配。 Trouble reading number from file (row 1u, field 1u) ==> ColumnA,ColumnB,ColumnC,ColumnD,ColumnE\\n 从文件读取错误的编号(行1u,字段1u)==> ColumnA,ColumnB,ColumnC,ColumnD,ColumnE \\ n

Error in csvread (line 48) m=dlmread(filename, ',', r, c); csvread中的错误(第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. 我在64位Windows 7计算机上运行MATLAB R2012a。 (Though I hope to upgrade to R2015b in the coming month) (尽管我希望在下个月升级到R2015b)

The reason is that you are using invalid csvread syntax. 原因是您使用的csvread语法无效。 From help csvread : help csvread

M = csvread('FILENAME') reads a comma separated value formatted file FILENAME. M = csvread('FILENAME')读取以逗号分隔的值格式的文件FILENAME。 The result is returned in M. The file can only contain numeric values. 结果以M返回。文件只能包含数字值。

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)从R行和C列开始以逗号分隔值格式的文件读取数据。R和C从零开始,因此R = 0和C = 0指定了第一个值文件。

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. M = csvread('FILENAME',R,C,RNG)仅读取RNG = [R1 C1 R2 C2]指定的范围,其中(R1,C1)是要读取的数据的左上角,而(R2, C2)是右下角。 RNG can also be specified using spreadsheet notation as in RNG = 'A1..B7'. 还可以使用电子表格符号指定RNG,如RNG ='A1..B7'。

You cannot tell csvread what delimiter to use. 无法告诉csvread使用什么定界符。 When you call it that way, it interprets ' as a numeric 44 (in ascii) and uses that as starting line. 当您以这种方式调用它时,它会将'解释为数字44(以ascii格式)并将其用作起始行。

Call this instead: data = csvread('output.csv',1); 而是这样调用: data = csvread('output.csv',1);

First, on MATLAB 2014b, I have a different error 首先,在MATLAB 2014b上,我遇到了另一个错误

Error using dlmread (line 138) HeaderLines must be integer-valued. 使用dlmread时出错(第138行)标头行必须为整数值。 Error in csvread (line 47) m=dlmread(filename, ',', r, c); csvread中的错误(第47行)m = dlmread(filename,',',r,c);

In MATLAB, I usually import data through its data import interface. 在MATLAB中,我通常通过其数据导入接口导入数据。 On Home tab, click on Import Data , there you have many options to make sure data import is correct. Home标签上,点击Import Data ,您可以使用许多选项来确保数据导入正确。 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. 您可以选择生成MATLAB实际用于导入数据的代码。 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. 如您所见,您可以轻松地修改代码以根据需要定制导入功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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