2010-05-18 51 views
17

我有一个CSV文件,我想读取这个文件并对每行进行一些预先计算,以查看该行对我是否有用,如果是,我保存它到一个新的CSV文件。 有人可以给我一个例子吗? 更详细,这是我的数据如何看起来像:(字符串,浮点数,浮点数)的数字是坐标。在MATLAB中逐行读取文本文件

ABC,51.9358183333333,4.183255 
ABC,51.9353866666667,4.1841 
ABC,51.9351716666667,4.184565 
ABC,51.9343083333333,4.186425 
ABC,51.9343083333333,4.186425 
ABC,51.9340916666667,4.18688333333333 

基本上我想保存新的文件中距离超过50或50的行。字符串字段也应该被复制。 感谢

+1

那么,对于这样的事情,我会用'awk'代替Matlab的 – Adrien 2010-05-18 14:19:30

+1

@Adrien - 我同意:AWK“BEGIN {FS =”, “} $ 2> = 50 {print $ 0}' output.csv – Adrian 2010-05-18 15:24:00

回答

9

实际上,你可以使用xlsread做到这一点。在文件'input_file.csv'首先将上述示例数据后,这里是你如何可以从三个输出从xlsread得到的数值,文本值,并在文件中的原始数据的例子:

>> [numData,textData,rawData] = xlsread('input_file.csv') 

numData =  % An array of the numeric values from the file 

    51.9358 4.1833 
    51.9354 4.1841 
    51.9352 4.1846 
    51.9343 4.1864 
    51.9343 4.1864 
    51.9341 4.1869 


textData = % A cell array of strings for the text values from the file 

    'ABC' 
    'ABC' 
    'ABC' 
    'ABC' 
    'ABC' 
    'ABC' 


rawData =  % All the data from the file (numeric and text) in a cell array 

    'ABC' [51.9358] [4.1833] 
    'ABC' [51.9354] [4.1841] 
    'ABC' [51.9352] [4.1846] 
    'ABC' [51.9343] [4.1864] 
    'ABC' [51.9343] [4.1864] 
    'ABC' [51.9341] [4.1869] 

你然后可以对数字数据执行所需的任何处理,然后使用xlswrite将数据行的子集重新保存到新文件中。这里有一个例子:

index = sqrt(sum(numData.^2,2)) >= 50; % Find the rows where the point is 
             % at a distance of 50 or greater 
             % from the origin 
xlswrite('output_file.csv',rawData(index,:)); % Write those rows to a new file 
+0

如果线条长度不同,这种方式是否可行? – Arun 2017-01-11 09:23:20

+0

@Arun:是的,它只会在'NaN'中填写缺少的数字值和''''以填充缺失的文本。 – gnovice 2017-01-11 14:38:52

+0

谢谢你的回复。当我尝试这个时,我实际上得到一个错误,即.xls文件的格式不正确。不过,我用dlmread与原始文本文件和一切工作。 – Arun 2017-01-11 18:07:04

3

这里是读取CSV商务部:http://www.mathworks.com/access/helpdesk/help/techdoc/ref/csvread.html 并写:http://www.mathworks.com/access/helpdesk/help/techdoc/ref/csvwrite.html

编辑

的作品的一个例子:

FILE.CSV:

 
1,50,4.1 
2,49,4.2 
3,30,4.1 
4,71,4.9 
5,51,4.5 
6,61,4.1 

该代码:

 
File = csvread('file.csv') 
[m,n] = size(File) 
index=1 
temp=0 
for i = 1:m 
    if (File(i,2)>=50) 
     temp = temp + 1 
    end 
end 
Matrix = zeros(temp, 3) 

for j = 1:m 
    if (File(j,2)>=50) 
     Matrix(index,1) = File(j,1) 
     Matrix(index,2) = File(j,2) 
     Matrix(index,3) = File(j,3) 
     index = index + 1 
    end 
end 
csvwrite('outputFile.csv',Matrix) 

和输出文件的结果:

 
1,50,4.1 
4,71,4.9 
5,51,4.5 
6,61,4.1 

这是不是可能是最好的解决方案,但它的工程!我们可以读取CSV文件,控制每行的距离并将其保存在新文件中。

希望它会帮助!

+2

从文档:”'csvread'将在未来的版本中被删除,请改用'dlmread'。 – 2010-05-18 14:15:22

+1

不幸的是,csvread不会读取字符。如果它只是数字,那么它很好。然而,只有数字,你不需要所有的循环,所有你需要的是 File = csvread('file.csv');csvwrite('outputFile.csv',File(File(:,2)> = 50,:)); 逻辑寻址将拉出列2大于等于50的所有行。 – Adrian 2010-05-18 14:41:48

+0

它也听起来像它不处理引号,换行符等,就像你期望的CSV函数一样。请参阅CSV RFC:http://www.ietf.org/rfc/rfc4180.txt – 2010-05-18 14:52:00

7

如果你真的想处理由行文件行,一个解决办法可能是使用fgetl

  1. 公开赛fopen
  2. 数据文件读取下一行到一个字符数组使用fgetl
  3. Retreive你所需要的字符数组使用sscanf数据你刚才读
  4. 执行任何相关的测试
  5. 输出你想要的文件
  6. 如果还没有到达文件末尾,请返回第2点。

与以前的答案不同,这不是很符合Matlab的风格,但它可能对非常大的文件更有效。

希望这会有所帮助。

7

您无法使用csvread读取文本字符串。 这里是另一种解决方案:

fid1 = fopen('test.csv','r'); %# open csv file for reading 
fid2 = fopen('new.csv','w'); %# open new csv file 
while ~feof(fid1) 
    line = fgets(fid1); %# read line by line 
    A = sscanf(line,'%*[^,],%f,%f'); %# sscanf can read only numeric data :(
    if A(2)<4.185 %# test the values 
     fprintf(fid2,'%s',line); %# write the line to the new file 
    end 
end 
fclose(fid1); 
fclose(fid2); 
5

在一块刚看了它以MATLAB

fid = fopen('file.csv'); 
data=textscan(fid,'%s %f %f','delimiter',','); 
fclose(fid); 

然后,您可以使用逻辑寻址

ind50 = data{2}>=50 ; 

ind50处理它,然后行的索引其中第2列大于50.所以

data{1}(ind50) 

将列出感兴趣的行的所有字符串。 就用fprintf将数据写出到新文件