MATLAB Answers


Read row x to row y in a textfile

Marthe Fenne Vestly さんによって質問されました 2017 年 10 月 27 日
最新アクティビティ Cedric Wannaz
さんによって コメントされました 2017 年 11 月 2 日
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)]
for i= 1:length(T)
fprintf(f1,'%11.4f %11.4f\n',[T(i) PSA(i)]);
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 件のコメント

サインイン to comment.

1 件の回答

Cedric Wannaz
回答者: Cedric Wannaz
2017 年 10 月 27 日
編集済み: Cedric Wannaz
2017 年 10 月 27 日

Is the following working?
[T1,PSA1]=textread('FFC_M7_1.txt', '%f %f %*s %*s','headerlines',32781);
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 件のコメント

Cedric Wannaz
2017 年 11 月 2 日
We are probably not working with the same file. This is what I have:
Marthe Fenne Vestly 2017 年 11 月 2 日
You are right, I was comparing two different files. Sorry about that, and thanks a lot.
Cedric Wannaz
2017 年 11 月 2 日
My pleasure!

サインイン to comment.

Translated by