【问题标题】:Matlab - Read multiple lines, where number of lines specified, from a text file and store in matrixMatlab - 从文本文件中读取多行,其中指定的行数并存储在矩阵中
【发布时间】:2016-08-09 04:09:19
【问题描述】:

我有一个文本文件,假设是 myfile.txt,它以下列方式存储浮点坐标:

53
-464.000000 -20.000000 0.009000
-464.000000 -17.000000 0.042000
-464.000000 -13.000000 0.074000
-464.000000 -11.000000 0.096000
-464.000000 -8.000000 0.114000
...
...
...
42
380.000000 193.000000 7.076000
381.000000 190.000000 7.109000
383.000000 186.000000 7.141000
384.000000 184.000000 7.163000
384.000000 183.000000 7.186000
386.000000 179.000000 7.219000
...
...
...

第一行指定第一组坐标的行数,然后是该行数。然后是另一个整数,指定下一组坐标的行数。

即第一行有 53 个,所以接下来的 53 行是第一组坐标(在第 54 行结束)。然后第 55 行的值为 42,因此接下来的 42 行是第二组坐标。

如何读取文本文件以便读取第一行,然后读取接下来的 53 行并将其存储在矩阵中。然后读取 42 并读取并存储接下来的 42 行?文本文件在 EOF 之前是这样的。

【问题讨论】:

    标签: matlab file matrix text lines


    【解决方案1】:

    你可以用Low-Level File I/O 来做,像这样:

    fid = fopen('myfile.txt', 'r');
    matrix = {};
    while ~feof(fid)
        N = fscanf(fid, '%d\n', 1);
        matrix{end + 1} = fscanf(fid, '%f\n', [3 N])';
    end
    fclose(fid);
    

    【讨论】:

    • 嗨,我运行了代码,发生的事情是文本文件中的行是垂直存储的。即从上面的文本文件中,矩阵以-464的形式垂直存储; -20; 0.009; -464; -17; 0.042;另外,我想保存出现在单独矩阵中的行数之后的坐标。因此,例如从上面的文本文件中,应该返回 2 个存储坐标的矩阵
    • 我已经编辑了我的答案以考虑这一点。现在matrix 是一个包含所有矩阵的元胞数组。
    【解决方案2】:
    fid = fopen('myfile.txt ', 'r');
    ind = 1;
    mycell = {};
    while ~feof(fid)
        N = fscanf(fid, '%d\n', 1);
        matrix = fscanf(fid, '%f\n', [3 N]);
        matrix = transpose(matrix);
        cell{ind} = matrix;
        ind = ind + 1;
    end
    fclose(fid);
    

    【讨论】:

    • 感谢您的贡献,但是对于另一个答案的如此微小的变化应该被表示为对另一个答案的评论,而不是发布一个完整的新答案。
    • 但是,上面的答案不会将不同的坐标集存储在不同的矩阵中。有什么办法吗?
    • @Hoki 我应该删除我的答案并将其发布在上一个答案的评论中吗?
    • 是的,最好删除这个答案,特别是因为您添加的代码执行了原始问题中没有特别要求的事情。但是,为了不丢失有用的信息(您的附加代码),我建议您在问题的末尾将其作为编辑发布,并附上评论说明您通过 Rafael 的回答解决了您的问题,并且您扩展了它以适应所需的输出格式。
    【解决方案3】:

    我的两分钱:

    function matrix_cells = import_coordinates(filename, startRow, endRow)
    %IMPORT_COORDINATES Import numeric data from a text file as a matrix.
    %   MATRIX_CELLS = IMPORT_COORDINATES(FILENAME) Reads data from text file FILENAME
    %   for the default selection and store it into the array of cells MATRIX_CELLS.
    %
    %   MATRIX_CELLS = IMPORT_COORDINATES(FILENAME, STARTROW, ENDROW) Reads data from
    %   rows STARTROW through ENDROW of text file FILENAME.
    %
    % Example:
    %   matrix_cells = import_coordinates('coordinates.txt', 1, 15);
    %
    %    See also TEXTSCAN.
    
    %% Initialize variables.
    delimiter = ' ';
    if nargin<=2
        startRow = 1;
        endRow = inf;
    end
    
    %% Format string for each line of text:
    %   column1: double (%f)
    %   column2: double (%f)
    %   column3: double (%f)
    % For more information, see the TEXTSCAN documentation.
    formatSpec = '%f%f%f%[^\n\r]';
    
    %% Open the text file.
    fileID = fopen(filename,'r');
    
    %% Read columns of data according to format string.
    % This call is based on the structure of the file used to generate this
    % code. If an error occurs for a different file, try regenerating the code
    % from the Import Tool.
    dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true, 'EmptyValue' ,NaN,'HeaderLines', startRow(1)-1, 'ReturnOnError', false);
    for block=2:length(startRow)
        frewind(fileID);
        dataArrayBlock = textscan(fileID, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true, 'EmptyValue' ,NaN,'HeaderLines', startRow(block)-1, 'ReturnOnError', false);
        for col=1:length(dataArray)
            dataArray{col} = [dataArray{col};dataArrayBlock{col}];
        end
    end
    
    %% Close the text file.
    fclose(fileID);
    
    %% Post processing for unimportable data.
    % No unimportable data rules were applied during the import, so no post
    % processing code is included. To generate code which works for
    % unimportable data, select unimportable cells in a file and regenerate the
    % script.
    
    %% Create output variable
    matrix_temp = [dataArray{1:end-1}];
    
    %% MY CONTRIBUTION ;)
    % find rows with nan values
    [i,~]=find(isnan(matrix_temp)); 
    indexes=unique(i); 
    
    %% Create output cell array
    matrix_cells=cell(numel(indexes),1);
    
    %% Fill cell array filtering out unuseful rows and parting original matrix
    for i=1:1:numel(indexes)-1
        matrix_cells{i}=matrix_temp(indexes(i)+1:indexes(i+1)-1,:);
    end
    % Last matrix
    matrix_cells{end}=matrix_temp(indexes(end)+1:size(matrix_temp,1),:);
    

    此函数的第一部分(文本文件的读取和导入)由 MATLAB 自动生成。我只添加了将矩阵拆分并保存到元胞数组中的代码。

    我假设坐标不包含 nan 值;此外,我不检查声明的行数与每个块的实际行数之间的一致性。

    这里是一个例子;以下是文件coordinates.txt

    5
    -464.000000 -20.000000 0.009000
    -464.000000 -17.000000 0.042000
    -464.000000 -13.000000 0.074000
    -464.000000 -11.000000 0.096000
    -464.000000 -8.000000 0.114000
    3
    380.000000 193.000000 7.076000
    381.000000 190.000000 7.109000
    383.000000 186.000000 7.141000
    2
    384.000000 184.000000 7.163000
    384.000000 183.000000 7.186000
    1
    386.000000 179.000000 7.219000
    

    执行函数:

    coordinate_matrices=import_coordinates('coordinates.txt')
    
    coordinate_matrices = 
    
        [5x3 double]
        [3x3 double]
        [2x3 double]
        [1x3 double]
    

    这是每个单元格的内容:

    >> coordinate_matrices{1}
    
    ans =
    
     -464.0000  -20.0000    0.0090
     -464.0000  -17.0000    0.0420
     -464.0000  -13.0000    0.0740
     -464.0000  -11.0000    0.0960
     -464.0000   -8.0000    0.1140
    
    >> coordinate_matrices{2}
    
    ans =
    
      380.0000  193.0000    7.0760
      381.0000  190.0000    7.1090
      383.0000  186.0000    7.1410
    
    >> coordinate_matrices{3}
    
    ans =
    
      384.0000  184.0000    7.1630
      384.0000  183.0000    7.1860
    
    >> coordinate_matrices{4}
    
    ans =
    
      386.0000  179.0000    7.2190
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-22
      • 2020-02-09
      • 1970-01-01
      • 2013-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多