Read row x to row y in a textfile
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi,
I have a very long textfile that I want to read the lines from 32781 to 32894, excluding the lines above and below, and store the lines in a new text files. I have made this code, but it does not work.
The txt-file have some lines with two columns and other with three and four. The lines I am interested in only have two columns.
[T1,PSA1]=textread('FFC_M7_1.txt', '%f %f','headerlines',32781);
[T,PSA] = [T1(32782:32894), PSA1(32782:32894)]
f1=fopen('FFC_M7_1_test.txt','w');
for i= 1:length(T)
fprintf(f1,'%11.4f %11.4f\n',[T(i) PSA(i)]);
end
fclose(f1);
This message appear when I try to run the code:
Error using dataread
Trouble reading floating point number from file (row 114, field 1) ==> Frequency (Hz) A
Error in textread (line 174)
[varargout{1:nlhs}]=dataread('file',varargin{:}); %#ok<REMFF1>
Error in WritePSA (line 1)
[T1,PSA1]=textread('FFC_M7_1.txt', '%f %f','headerlines',32781);
Does anyone know how to do this?
Thanks, Marthe
댓글 수: 0
채택된 답변
Cedric
2017년 10월 27일
편집: Cedric
2017년 10월 27일
Is the following working?
[T1,PSA1]=textread('FFC_M7_1.txt', '%f %f %*s %*s','headerlines',32781);
or
content = fileread( 'FFC_M7_1_test.txt' ) ;
lineStarts = [0, strfind( content, sprintf('\n') )] + 1 ;
block = content(lineStarts(32782) : (lineStarts(32895)-1)) ;
data = reshape( str2double( regexp( block, '[\d\.\-]+', 'match' )), 2, [] ).' ;
I cannot test right now though.
댓글 수: 14
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Low-Level File I/O에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!