2014-07-17 55 views
0

當我導入數據矩陣時,在第一列的第一行中,每獲取一次新數據就會有一個標記,這個標記會干擾MATLAB如何導入數據。如何使用標記導入數據 - 但不包括那些標記?

有沒有辦法對此進行編碼?

例如:

「> 1 6 1 1 -0.00161

1 6 1 2 -0.00140

1 6 1 3 -0.00145

1 6 1 4 -0.00153

1 6 1 5 -0.00120

1 6 1 6 -0.00076

我寧願不要手動從數據中刪除>,因爲數據可能會有數千個。

+0

從你在導入數據?並以什麼格式? – EJG89

+0

我正在從txt文件導入數據,然後嘗試對數據和其他數據進行排序。但是「>」是代碼中的猴子扳手 – user3826497

回答

0

如果您在* nix系統下或者您有cygwin,那麼如果您將此輸出發送到命令sed,則可以清除這些>。例如:

[email protected] $ cat out.txt 
>0 5 3 4 

0 6 4 3 

>1 5 3 6 

1 2 4 5 
[email protected] $ cat out.txt |sed 's/>//g' 

如果你需要這個新的輸出到文件存儲: 用戶@主機$貓out.txt

0 6 4 3 

>1 5 3 6 

1 2 4 5 
[email protected] $ cat out.txt |sed 's/>//g' > out_without_unneeded_symbols.txt 
[email protected] $ cat out_without_unneeded_symbols.txt 
0 5 3 4 

0 6 4 3 

1 5 3 6 

1 2 4 5 

如果此輸出取自當前目錄中的某個程序:

[email protected] $ ./some_program |sed 's/>//g' 
+0

我無法重複您的結果,但我有一種感覺,那就是我的問題。我確實發現使用手動導入選項,數據將被導入w/o「>」,是否有一種簡單的方法在導入過程中將其編程到matlab中,還是隻有在手動導入時纔可能? – user3826497

0

這是在MATLAB一個可能的實現:

% read file lines as a cell array of strings 
fid = fopen('file.dat', 'rt'); 
C = textscan(fid, '%s', 'Delimiter',''); 
C = C{1}; 
fclose(fid); 

% find marker locations 
markers = strncmp('>', C, 1); 

% remove markers 
C = regexprep(C, '^>', ''); 

% parse numbers into a numeric matrix 
X = regexp(C, '\s+', 'split'); 
X = str2double(vertcat(X{:})); 

結果:

% the full matrix 
>> X 
X = 
    0  5  3  4 
    0  6  4  3 
    1  5  3  6 
    1  2  4  5 

% only the marked rows 
>> X(markers,:) 
ans = 
    0  5  3  4 
    1  5  3  6 
+0

如果在矩陣中的數字是 1 0.05 4 5 1 66 1.5 4 等vertcat會給我一個錯誤的做尺寸。有沒有快速解決這個問題? 謝謝! – user3826497

+0

請在問題中發佈此數據的示例(格式正確)。以前顯示的數據正常工作。 – Amro

+0

我用更好的例子解決了這個問題。我道歉。我忘了touchy matlab是如何得到的 – user3826497