How can we skip specific string with textscan or textread?

Hello,
I have a file .txt which contains the following text:
VT970103 KOR T. 5 P P2 P2
JAS
CTC N VT980023 GRA T. 6
VT943333 MAI A. 7 P1 P2 P2
JC1 VT910209 MER T. 8 P U2 P
JAS
CTC VT965366 KIR M. 9 P2
RH856957 DIOP C. 10 P2 P2 P P
VT860609 KOR M. (CAP) 11 P P P P1
JC1 VT940239 BER R. 12 P2
JC1 OH883091 FAL M. 14 P2 P2 T1
VT900521 JEN T. 15 P P P2 T1
VT740910 JAC J.TC
I would like to extract three datas of each line. For example for the first line: 'VT970103' , 'KOR T' and '5'.
I use the following function textscan:
fid = fopen( 'text.txt', 'r' );
lic = textscan(fid, '%s %[ABCDEFGHIJKLMNOPQRSTUVWXYZ ] %[^1234567890] %d %*[^\n]')
fclose(fid);
It works when the line has the same format as the first line. But I would like to skip the second line for example because there is not any interesting information. For the third line I would like to skip 'CTC N', and begin to read at VT, and so on.
The common feature to begin to read a line would be when you have a string which contains 2 letters and 6 figures one after the other without any whitespace.
I hope my description is enough clear.
Thank you for your help.

 採用された回答

Walter Roberson
Walter Roberson 2016 年 11 月 11 日

0 投票

I suggest reading the entire file using fileread() and then using
data = regexp(TheFileContent, '^(?<flight>\D\D\d{6})\s+(?<nom>\D*?)\s+(?<num>\d+)', 'names', 'lineanchors');
the result should be a struct array with fields "flight", "nom", and "num"
(I am guessing about what the fields mean)

1 件のコメント

Gauvain
Gauvain 2016 年 11 月 11 日
Thank you so much. This is exactly what I expected.

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

その他の回答 (1 件)

Jan
Jan 2016 年 11 月 11 日

0 投票

Read the file line by line using fgetl. Then split the first word by strtok and check, if it matchs the pattern. If so, you can parse it by your textscan command.

1 件のコメント

Sagar Talaviya
Sagar Talaviya 2018 年 1 月 23 日
編集済み: Walter Roberson 2018 年 1 月 23 日
I do have doubts with textscan and regexp. will you help? link to my question: https://de.mathworks.com/matlabcentral/answers/378134-problem-regular-expression-to-analyse-text-file

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

カテゴリ

ヘルプ センター および File ExchangeText Data Preparation についてさらに検索

質問済み:

2016 年 11 月 11 日

編集済み:

2018 年 1 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by