2016-05-17 154 views
0

我使用csvread命令讀取CSV文件較大:如何通過跳過錯誤的行在MATLAB中加載csv文件?

M=csvread('myfile.csv'); 

但也有它幾排,很顯然,這不允許MATLAB加載因爲是文本文件(或其他垃圾) 。例如,行號45372,117573等。那麼,如何在加載文件時跳過它們?

+0

你收到了什麼錯誤信息? –

回答

0

很少意味着已經很少,你可以使用try-catch結構在while循環。還需要知道列的數量。

下面的代碼預期錯誤消息:

Mismatch between file and format string. Trouble reading number from file (row <row#>, field <field#>) ==> <faulty line content>

代碼試圖加載整個csv的一次。如果失敗,則分析錯誤消息,識別故障線路,將文件加載到之前的行並將結果追加到data變量。在下一次運行中,它開始在故障線下面加載一行。當剩餘文件成功讀取到最後時,此迭代終止。

StartRow=<number of ignored rows (header)>; 
Ncols=<width>; 
data=zeros(0,Ncols); 
csvFault=true; 
while csvFault 
    try 
    Temp=csvread('YourFile.csv',StartRow,0)  % Try to read file from StartRow to the end-of-file. 
    csvFault=false;        % Executed only when CSVread is succesful 
    catch msg 
    %% Error occured 
    faultyRow=regexp(msg.message,' ','split'); % Split error message into cells 
    faultyRow=faultyRow{12};      % 12th cell contain row identifier 
    faultyRow=str2double(faultyRow(1:end-1)); % Identifier is number followed by 1 letter 

    %% Read the data from the segment between faulty lines 
    Temp=csvread('YourFile.csv',StartRow,0,[StartRow,0,faultyRow-1,Ncols-1]) 

    StartRow=faultyRow+1; 
    end 
    data=[data;Temp]; 
end