MATLAB Answers

Gauvain
0

How can we skip specific string with textscan or textread?

Asked by Gauvain
on 11 Nov 2016
Latest activity Edited by Walter Roberson
on 23 Jan 2018
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.

  0 Comments

Sign in to comment.

2 Answers

Answer by Walter Roberson
on 11 Nov 2016
 Accepted Answer

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 Comment

Thank you so much. This is exactly what I expected.

Sign in to comment.


Answer by 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.

  1 Comment

Sign in to comment.