Extract n lines from a text file

2 ビュー (過去 30 日間)
Luciano Raso
Luciano Raso 2012 年 6 月 1 日
Hi Everybody, i am struggling with the following problem. I need to extract, from a text file (actually an xml file, anyway) the first n lines. I know there are some functions (as fscanf) in which the user can specify the number of characters to extract, but in my case this number is variable, therefore i cannot specify a priori where it is.
An alternative could be: finding the "ending character" by identifying some key word, and then using this ending character in fscanf. But i did not find a way to identify pieces of text within a longer text.
Does anyone know a possible solution? Luciano
  2 件のコメント
Oleg Komarov
Oleg Komarov 2012 年 6 月 1 日
Is "n" fixed?
Thomas
Thomas 2012 年 6 月 1 日
Can you give an example file where you want to remove the first n lines..

サインインしてコメントする。

回答 (1 件)

per isakson
per isakson 2012 年 6 月 1 日
Something like this might work
fid = fopen( 'cssm.txt', 'r' );
str = permute( fread( fid, '*char' ), [2,1] );
sts = fclose( fid );
cac = regexp( str, 'your_key_word', 'split' );
buf = strtrim(cac{2});
ca = textscan( buf, frmt );
or replace regexp by strfind

カテゴリ

Help Center および File ExchangeText Data Preparation についてさらに検索

タグ

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by