MATLAB Answers

How can we skip specific string with textscan or textread?

12 views (last 30 days)
Gauvain on 11 Nov 2016
Edited: Walter Roberson on 23 Jan 2018
I have a file .txt which contains the following text:
VT970103 KOR T. 5 P P2 P2
CTC N VT980023 GRA T. 6
VT943333 MAI A. 7 P1 P2 P2
JC1 VT910209 MER T. 8 P U2 P
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]')
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.

Accepted Answer

Walter Roberson
Walter Roberson on 11 Nov 2016
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)

More Answers (1)

Jan on 11 Nov 2016
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.

Community Treasure Hunt

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

Start Hunting!