【问题标题】:Reading a text file in MATLAB line by line在MATLAB中逐行读取文本文件
【发布时间】:2010-05-18 10:42:02
【问题描述】:

我有一个 CSV 文件,我想读取该文件并对每一行进行一些预计算,以查看该行是否对我有用,如果是,我将其保存到一个新的 CSV 文件中。 有人可以给我一个例子吗? 更详细地说,这是我的数据的样子:(string,float,float) 数字是坐标。

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 的行保存在一个新文件中。字符串字段也应该被复制。 谢谢

【问题讨论】:

  • 好吧,对于这种事情,我将使用awk而不是Matlab
  • @Adrien - 我同意:awk 'BEGIN{FS=","}$2>=50{print $0}' output.csv

标签: file matlab file-io csv


【解决方案1】:

您实际上可以使用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

【讨论】:

  • @Arun:是的,它会简单地填写 NaN 用于缺少数值,'' 用于缺少文本。
  • 感谢您的回复。当我尝试这个时,我实际上得到一个错误,即 .xls 文件的格式不正确。但是,我将 dlmread 与原始文本文件一起使用,一切正常。
  • @Arun:没有看到您的文件是什么样子,恐怕我无法提供进一步的帮助。不过,很高兴您在 dlmread 中找到了解决方案。
【解决方案2】:

如果你真的想逐行处理你的文件,一个解决方案可能是使用fgetl:

  1. fopen打开数据文件
  2. 使用fgetl将下一行读入字符数组
  3. 在刚刚读取的字符数组上使用sscanf 检索您需要的数据
  4. 执行任何相关测试
  5. 将您想要的内容输出到另一个文件
  6. 如果您还没有到达文件末尾,请返回第 2 点。

与上一个答案不同,这不是 Matlab 的风格,但它可能对非常大的文件更有效。

希望这会有所帮助。

【讨论】:

    【解决方案3】:

    您无法使用 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);
    

    【讨论】:

      【解决方案4】:

      一口气读入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 将您的数据写入新文件

      【讨论】:

        【解决方案5】:

        这里是读取 csv 的文档:http://www.mathworks.com/access/helpdesk/help/techdoc/ref/csvread.html 并写:http://www.mathworks.com/access/helpdesk/help/techdoc/ref/csvwrite.html

        编辑

        一个有效的例子:

        文件.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

        代码:

        文件 = csvread('file.csv') [m,n] = 大小(文件) 索引=1 温度=0 对于 i = 1:m 如果(文件(i,2)>=50) 温度 = 温度 + 1 结尾 结尾 矩阵 = 零(温度,3) 对于 j = 1:m 如果(文件(j,2)>=50) 矩阵(索引,1)=文件(j,1) 矩阵(索引,2)=文件(j,2) 矩阵(索引,3)=文件(j,3) 索引 = 索引 + 1 结尾 结尾 csvwrite('outputFile.csv',矩阵)

        以及输出文件结果:

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

        这可能不是最好的解决方案,但它确实有效!我们可以读取 CSV 文件,控制每一行的距离,并保存到一个新文件中。

        希望对您有所帮助!

        【讨论】:

        • 来自文档:“csvread 将在未来的版本中被删除。改用dlmread。”
        • 不幸的是 csvread 不读取字符。如果只是数字,那很好。然而,只有数字你不需要所有的循环,你只需要 File = csvread('file.csv'); csvwrite('outputFile.csv',File(File(:,2)>=50,:));逻辑寻址将提取第 2 列 >=50 的所有行。
        • 听起来它也不像您期望的 CSV 函数那样处理引号、换行符等。请参阅 CSV RFC:ietf.org/rfc/rfc4180.txt
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-07
        • 1970-01-01
        • 2014-12-01
        • 2015-10-25
        • 2011-07-23
        • 2012-10-11
        相关资源
        最近更新 更多