2014-01-31 64 views
0

我從一個文本文件中的字符串字符串複製文本:從滿足一定條件MATLAB

20130806_083642832,!AIVDM,1,1,,B,13aFeA0P00PEqQNNC4Um7Ow`[email protected],0*5E 

20130806_083643032,!AIVDM,2,1,4,B,E>jN6<[email protected]@LtP0000:uoH?9Ur,0*50 

我需要去通過人物,並在開始提取日期,那麼,經過啓動消息B,(但也可能是A,)直到,0

有什麼想法?

回答

0

好,有更優雅的方式來解決這個問題,但我下面的例子會給你如何操作字符串在MatLab的感覺(這可能是你有問題的東西)。在這裏你去:

String='20130806_083642832,!AIVDM,1,1,,B,13aFeA0P00PEqQNNC4Um7Ow`[email protected],0*5E' 

for i=1:length(String) 
    if(strcmp(String(i),'B'))  %or strcmp(String(i),'A') 
     for j=i:length(String) %or "for j=length(String):i" if you meant the last 0 ;) 
      if(strcmp(String(j),'0')) 

       String2=String(i:j) 

       break 
      end 
     end 
     break 
    end 
end 

輸出

String = 

20130806_083642832,!AIVDM,1,1,,B,13aFeA0P00PEqQNNC4Um7Ow`[email protected],0*5E 


String2 = 

B,13aFeA0 

只是玩弄字符串索引與strcmpstrcmpi,你會得到一個感覺,就能寫出更漂亮的表達。

現在嘗試自行提取的日期!

希望有幫助!

0

沒有循環,你可以做這樣的事情:

startString = ['20130806_083642832,!AIVDM,1,1,,B,13aFeA0P00PEqQNNC4Um7Ow`[email protected],0*5E']; 
startPosition = find(startString == 'B') + 1; 
if ~startPosition 
    startPosition = find(startString == 'A') + 1; 
end 
tmpMessage = startString(startPosition:end); 

endPosition = find(tmpMessage == '0') - 1; 
outMessage = tmpMessage(1:endPosition(1)) 

dateString = startString(1:8) 

這使輸出:

outMessage = ,13aFeA 
dateString = 20130806