您可以使用low level IO和regular expressions來讀取您的*.rtf
文件,並在沒有任何轉換的情況下獲取您的數據。使用您的示例數據和*.rtf
文件,我彙集了一個笨重的解析器,它可以爲您提供數據。如果您在文本編輯器打開你的*.rtf
文件,你會發現(至少在我的)它有2個標題行:
{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
{\*\generator Riched20 6.3.9600}\viewkind4\uc1
其次是與您的數據混合在一起多一點頭(可能只是一個寫字板失敗):
\pard\sa200\sl276\slmult1\f0\fs22\lang9 1.0E-5 78.29777\par
所以我們跳過前兩行,區別對待的第三行,然後處理其餘部分:
fID = fopen('test.rtf', 'r'); % Open our data file
nheaders = 2; % Number of full header lines
npartialheaders = 1; % Number of header lines with your data mixed in
ii = 1;
mydata = [];
while ~feof(fID) % Loop until we reach the end of the file
if ii <= nheaders
% Do nothing
tline = fgetl(fID); % Read in a line of data, discard it
ii = ii + 1;
else
tline = fgetl(fID); % Read in a line of data
out = regexp(tline, '([\s\d.E-])', 'match');
if ~isempty(out) % Our regex found some data
% The regexp returns every character in a cell, concatenate them
% and split them along the spaces
data_str = strsplit([out{:}], ' ');
if ii > nheaders && ii <= (nheaders + npartialheaders)
% Header is mixed with your data
% We should only want the second and third matches
data_num = str2double(data_str(2:3));
mydata = [mydata; data_num];
else
% Just your data on these lines
data_num = str2double(data_str(1:2));
mydata = [mydata; data_num];
end
end
ii = ii + 1;
end
end
fclose(fID);
將返回:
mydata =
1.00000000000000e-05 78.2977700000000
1.06250000000000e-05 75.9674000000000
1.12500000000000e-05 73.8342400000000
1.18750000000000e-05 71.8719700000000
1.25000000000000e-05 70.0589500000000
1.37500000000000e-05 66.8116000000000
1.50000000000000e-05 63.9797000000000
1.62500000000000e-05 61.4816700000000
無可否認,這是醜陋的,效率低下的代碼。我確信可以進行很多更改以使其更加健壯和高效,但它可以幫助您開始。
既然你有你的數據,我想你可以在搞清楚你的第二部分。如果你還沒有,請看看MATLAB的matrix indexing documentation。作爲一個實現的提示,請查看min
的輸出,並考慮您可以從矢量中減去常數。
% What is this doing? It's a mystery! [~, matchidx] = min(abs(mydata(:,1) - querypoint)); disp(mydata(matchidx, 2))
作品,謝謝! –