2013-08-28 76 views
3

我有存儲爲文本文件中的數據如下:Matlab的文本閱讀

1377303313.298551: 1377303312.800000, **40.497522, -74.434818, 0.026000,** PS 

1377303313.320448: R255 1 

1377303313.369429: R255 1 

1377303313.419430: R255 1 

1377303313.468416: R255 1 

1377303313.676656: 1377303313.200000, **40.497521, -74.434819, 0.010000** PS 

1377303313.715420: R255 1 

1377303313.814361: R255 1 

我想我的文件看起來像這樣:

1 255 **40.497522 -74.434818 0.026000** 1377303313.320448 

1 255 **40.497522 -74.434818 0.026000** 1377303313.369429 

1 255 **40.497522 -74.434818 0.026000** 1377303313.419430 

1 255 **40.497522 -74.434818 0.026000** 1377303313.468416 

1 255 **40.497521 -74.434819 0.010000** 1377303313.715420 

1 255 **40.497521 -74.434819 0.010000** 1377303313.814361 

總之,我需要大膽的部位要繼續進行直到他們進行下一次PS測量,然後重複同樣的事情。我真的很困難,我會感謝一些幫助!

在此先感謝。

+1

你能否證實有時是一個逗號您的數據後,PS之前,有時不是?或者是其中一行是錯字? – Geodesic

回答

1

我很快把一些腳本放在一起。我認爲你的輸入文件有一個固定的格式,這意味着值的長度在行之間不會改變。這樣可以更容易地從線條中提取所需的東西,比如「粗體部分」。如果它們不是固定的,則正規表達式可能是提取所需的,但腳本的一般結構不應改變。

disp(' '); 

fname = 'data.txt'; 

% desired output line format 
desiredlineTemplate = '1 255 %s %s\n'; 


boldPart = ''; 

fid = fopen(fname); 

% get first line 
tline = fgets(fid); 

if numel(tline) > 36 
    % check if first line in a file is a PS line 
    % based on its length. 
    % lines with PS are much longer than the lines without PS 
    % so we can use this information 

    % extract the "bold part" 
    boldPart = tline(39:end-4); 

    % remove commas 
    boldPart = strrep(boldPart,',',''); 
end 

while ischar(tline) 

    % disp(tline); 

    if numel(tline) == 1, 
     % enmpty lines have length of 1. At least 
     % when I tried this. If this is not the case for your 
     % text file, u can ignor this bit. 
     tline = fgets(fid); 
     continue; 
    end 


    % lines with PS are much longer than the lines without PS 
    % so we can use this information 
    if numel(tline) > 36 
     % this is a line with PS 
     % so extract "bold part" 
     boldPart = tline(39:end-4); 

     % remove comma 
     boldPart = strrep(boldPart,',',''); 

    else 
     % now we are in a line without PS. Thus need to reformat this line 
     % and add "bold part" extracted earlier. 

     % extract the first number in a line 
     % again based on ints length 
     firstNumber = tline(1:17); 

     % CONSTRUCT THE DISIRED LINE OUTPUT 
     desiredLine = sprintf(desiredlineTemplate, boldPart, firstNumber); 

     disp(desiredLine); 
    end 

    tline = fgets(fid); 

end 

fclose(fid); 

輸出是:

>> 
1 255 40.497522 -74.434818 0.026000 1377303313.320448 

1 255 40.497522 -74.434818 0.026000 1377303313.369429 

1 255 40.497522 -74.434818 0.026000 1377303313.419430 

1 255 40.497522 -74.434818 0.026000 1377303313.468416 

1 255 40.497521 -74.434819 0.010000 1377303313.715420 

1 255 40.497521 -74.434819 0.010000 1377303313.814361 
+0

如果這些數字的大小不一樣,那麼函數fscanf將會非常有用。事實上,fscanf會簡化上述過程,但我會把它留給別人來寫。 – Cramer

+0

@Cramer yep,fscanf也可以使用。 – Marcin

+0

Thnaks很多!線條之間的值的長度會改變,但我會用代碼來玩一下來解決!我很感激!! – user2727146