read .txt file data with data between specific lines "string" .

I want to read data between ( strings ) line starting from " *Node " and
ending at and next (strings ) line " *Element, type=C3D4" .

 採用された回答

Star Strider
Star Strider 2019 年 6 月 27 日

0 投票

The textscan function using a numeric format descriptor string will automatically stop when it encounters:
*Element, type=C3D4
so this is actually a straightforward problem:
fidi = fopen('Copy_of_Job-1.txt','rt');
D = textscan(fidi, '%f%f%f%f', 'HeaderLines',9, 'Delimiter',',', 'CollectOutput',1);
fclose(fidi);
with ‘D’ being a (1418x4) double matrix.

7 件のコメント

Star Strider
Star Strider 2019 年 6 月 27 日
With only two sections, and changing lines of text and changing format, your file needs specific and different textscan calls for the two sections:
fidi = fopen('Copy_of_Job-1.txt','rt');
D{1} = textscan(fidi, '%f%f%f%f', 'HeaderLines',9, 'Delimiter',',', 'CollectOutput',1);
txtline = fgets(fidi);
D{2} = textscan(fidi, '%f%f%f%f%f', 'Delimiter',',', 'CollectOutput',1);
fclose(fidi);
D1 = cell2mat(D{1}); % First Section
D2 = cell2mat(D{2}); % First Section
with ‘D1’ being a (1418 x 4) double, and ‘D2’ being a (6584 x 5) double.
Different files with different requirements would of course require different code.
jahanzaib ahmad
jahanzaib ahmad 2019 年 6 月 27 日
編集済み: jahanzaib ahmad 2019 年 6 月 27 日
thankyou got it .but i want code to find the next string itself and read the data upto next string .
jahanzaib ahmad
jahanzaib ahmad 2019 年 6 月 27 日
how can i find the line number with the matching strings ? e.g *nodes and **Element, type=C3D4
jahanzaib ahmad
jahanzaib ahmad 2019 年 6 月 27 日
編集済み: jahanzaib ahmad 2019 年 6 月 27 日
the file format in my case remains constant with same strings .its abaqus .inp file . i want to read the data between strings and modify it .i changed it to .txt .
Star Strider
Star Strider 2019 年 6 月 27 日
I am not certain what you are asking. I believe the code I posted in my Comment will do what you want.
If your file format remains the same, my code will work with all your files.
jahanzaib ahmad
jahanzaib ahmad 2019 年 6 月 27 日
thankyou . i got it .. . ur code works perfectly
Star Strider
Star Strider 2019 年 6 月 27 日
As always, my pleasure.
I appreciate your compliment.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Import and Analysis についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by