2017-03-27 52 views
1

我有一個包含超過一百萬行的數據文件,其中包含16個整數(它並不重要),我需要處理Octave中的行。顯然,加載整個文件是不可能的。我如何只加載特定的行?僅加載特定行

我想到了兩種可能性:在簡單的I/O

  • 的文檔

    • 我錯過了一些東西,我應該將文件轉換成爲一個CSV和使用一些csvread的特點
  • +0

    它是在一個文件中的任意直線? – Suever

    +0

    是的。重點是通過行來創建一個循環,而不是加載整個文件。 –

    +0

    所以你只想一行一行地做點什麼? – Suever

    回答

    3

    如果要逐行遍歷文件,可以打開文件,然後使用fscanf解析每行。

    fid = fopen(filename); 
    
    while true 
        % Read the next 16 integers 
        data = fscanf(fid, '%d', 16); 
    
        % Go until we can't read anymore 
        if isempty(data) 
         break 
        end 
    end 
    

    如果你想在每行一個字符串,您可以改用fgetl讓每一行

    fid = fopen(filename); 
    
    % Get the first line 
    line = fgetl(fid); 
    
    while line 
        % Do thing 
    
        % Get the next line 
        line = fgetl(fid); 
    end