Text file reading problem

4 views (last 30 days)
Oscar
Oscar on 9 Aug 2012
Hi all,
I would like to code a MATLAB function that could perform the following operations:
  • Read a given text file (see below)
  • Extract the numeric values corresponding, for example, to the variables: Alpha, pb/2v, Beta, qc/2V, Mach, rb/2v, [...], CLa, CLb, CYa, CYb, [...], Xnp.
  • Store (and return) these numeric values into a column vector or row vector. That is : [6.220 0.000 0.000 0.800 0.000 ... 3.770 0.000655 ... 10.885]
The problem is that the text file seems to be a bit complicated because it doesn't have a definite format.
Here is a sample text file:
---------------------------------------------------------------
Vortex Lattice Output -- Total Forces
Configuration: Airplane
# Surfaces = 12
# Strips = 81
# Vortices = 778
Sref = 122.40 Cref = 4.1935 Bref = 37.573
Xref = 14.390 Yref = 0.31229E-04 Zref =-0.40414
Standard axis orientation, X fwd, Z down
Run case: Cruise
Alpha = 6.22083 pb/2V = 0.00000 p'b/2V = 0.00000
Beta = 0.00000 qc/2V = 0.00000
Mach = 0.800 rb/2V = 0.00000 r'b/2V = 0.00000
CXtot = 0.02889 Cltot = -0.00001 Cl'tot = 0.00000
CYtot = -0.00006 Cmtot = 0.00000
CZtot = -0.48764 Cntot = 0.00003 Cn'tot = 0.00003
CLtot = 0.48790
CDtot = 0.02412
CDvis = 0.00000 CDind = 0.02412
CLff = 0.48991 CDff = 0.02138 | Trefftz
CYff = -0.00006 e = 0.3098 | Plane
Elevator = 8.77320
Rudder = 0.00000
---------------------------------------------------------------
Stability-axis derivatives...
alpha beta
---------------- ----------------
z' force CL | CLa = 3.770952 CLb = 0.000655
y force CY | CYa = -0.000593 CYb = -0.895943
x' mom. Cl'| Cla = -0.000003 Clb = -0.068255
y mom. Cm | Cma = 3.151285 Cmb = -0.003040
z' mom. Cn'| Cna = 0.000338 Cnb = 0.101545
roll rate p' pitch rate q' yaw rate r'
---------------- ---------------- ----------------
z' force CL | CLp = 0.000039 CLq = -2.410821 CLr = -0.000764
y force CY | CYp = -0.199752 CYq = -0.007101 CYr = 0.709363
x' mom. Cl'| Clp = -0.375950 Clq = -0.000470 Clr = 0.109807
y mom. Cm | Cmp = -0.000244 Cmq = -58.421581 Cmr = 0.003568
z' mom. Cn'| Cnp = -0.067254 Cnq = 0.003484 Cnr = -0.321462
Elevator d1 Rudder d2
---------------- ----------------
z' force CL | CLd1 = 0.008195 CLd2 = 0.000008
y force CY | CYd1 = 0.000001 CYd2 = -0.005356
x' mom. Cl'| Cld1 = 0.000000 Cld2 = -0.000690
y mom. Cm | Cmd1 = -0.038800 Cmd2 = -0.000039
z' mom. Cn'| Cnd1 = -0.000001 Cnd2 = 0.002880
Trefftz drag| CDffd1 = 0.001417 CDffd2 = 0.000001
span eff. | ed1 = -0.010030 ed2 = 0.000001
Neutral point Xnp = 10.885436
Clb Cnr / Clr Cnb = 1.967782 ( > 1 if spirally stable )
I tried to use the TEXTSCAN function but, because the format is not the same everywhere (in each line), the code is not very “clean” and efficient. I also tried to use the SSCANF function, combined with a “while loop”, in the following form (for example):
fid = fopen('Textfile.txt','r');
while ~feof(fid)
A{1} = sscanf(fid,'z'' force CL | CLa = %f CLb = %f');
A{2} = sscanf(fid,' y force CY | CYa = %f CYb = %f');
etc...
end
fclose(fid)
In which A is a cell array (or matrix). This doesn’t work because, each time the “while loop” restarts, the contents of the cell that was previously stored is cleared. Is it possible to read a text file line by line, and store the numerical data from each line into a cell array? Could you help me please ? Is there a faster and more efficient way for solving this "simple" problem?
Thanks in advance for your help!

Accepted Answer

Matt Kindig
Matt Kindig on 9 Aug 2012
Hi Oscar,
I've had a similar issue (reading data from a text file with an irregular format, and instead a series of "keys" that you need to find in the file). I solved it by using regular expressions (regex())--are you familiar with those. In general, I did it like this:
str = fileread(FILENAME_HERE); %read file into string
pieces = regexp(str, '\-{2,}', 'split'); %split into sections indicated by repeated hyphens
%then process each piece.
The big issue that could arise is if your file is especially long--MATLAB may not have enough memory to read it in full all at once.
Does this help? How much regexp() experience do you have?
  1 Comment
Oscar
Oscar on 10 Aug 2012
Hi Matt,
Thanks for answering my question! I'm not familiar with the REGEXP function but it seems to be an interesting way to solve the problem. I'm going to read the Matlab help carefully and I will try to implement this function. I will keep you informed.
Thanks again Matt

Sign in to comment.

More Answers (0)

Categories

Find more on Variables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!