2013-08-28 5 views
3

다음과 같이 나는 텍스트 파일로 저장된 데이터를 가지고 : 한마디로matlab에 텍스트 읽기

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 

, 내가 할 굵은 부분이 필요합니다

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 

나는 내 파일은 다음과 같이 할 다음 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