gpt4 book ai didi

matlab - csvread在MATLAB中删除行

转载 作者:行者123 更新时间:2023-12-02 09:31:36 26 4
gpt4 key购买 nike

我在5列中创建了一些随机数据,并将其存储为CSV文件。这个工作正常,我的CSV实际上包含1001行;我的数据带有标题(我通过在电子表格编辑器中打开它进行了检查)。但是,当我再次使用csvread在MATLAB中读取它时,如果我未指定定界符,并且当我指定定界符data仅包含957行时,则会出现错误,因此 header 和前42行丢失。这里发生了什么?

码:

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 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);



我在写或读文件时是否缺少一些愚蠢的东西?

我在64位Windows 7计算机上运行MATLAB R2012a。 (尽管我希望在下个月升级到R2015b)

最佳答案

原因是您使用了无效的csvread语法。从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'.



您无法告诉 csvread使用什么定界符。当您以这种方式调用它时,它将 '解释为数字44(以ascii表示),并将其用作起始行。

改称这个: data = csvread('output.csv',1);

关于matlab - csvread在MATLAB中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32552641/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com