2010-05-18 78 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將數據寫出到新文件