0
votes

I've been trying to obtain a variable from a text file which I read in within the Matlab workspace.

The file contains the following:

---------------------------------------------------------------
 Surface Forces (referred to Sref,Cref,Bref about Xref,Yref,Zref)
 Standard axis orientation,  X fwd, Z down         

     Sref =   35.00       Cref =    2.4325   Bref =   14.5000
     Xref =     18.5306   Yref =    0.0000   Zref =   -0.7092

 n      Area      CL      CD      Cm      CY      Cn      Cl     CDi     CDv
 1    35.263  0.6972  0.0138  4.8547  0.0040  0.0069 -0.2817  0.0138  0.0000   F27 WING

 Surface Forces (referred to Ssurf, Cave about root LE on hinge axis)

   n     Ssurf      Cave       cl       cd      cdv    cm_LE
   1    35.263     2.432   0.6920   0.0137   0.0000   0.0000  F27 WING
 ---------------------------------------------------------------

I need the value below CL, in this case its 0.6972. I've tried using fopen and importdata without succes. The importdata just puts the whole file in a cell array with 9 rows and 1 column containing all strings. From there I dont't know how to proceed further.

With the fopen, I've tried to read the file line by line and to check whether he finds the CL string. He does find it but the value its gives is [].

Can anyone give me a tip? Thank you.

2

2 Answers

0
votes

Use fgetl() to extract lines you don't need, then use fscanf() to read a line of data into a vector ('dataline'). Then you can access the individual elements of the vector.

Example based on your file:
Open and read file, discarding first 7 lines, including blank lines:

fid = fopen(filename, 'r')
for i = 1:7
    oneline = fgetl(fid);
end

read 8th line of file; store in a vector of floats

dataline = fscanf(fid, ['%f' ])

assign third value of vector to 'CL'

CL = dataline(3)
fclose(fid)

CL
ans =

    0.6972
0
votes

If you have the luxury of having one of the newer versions of Matlab, then the following will work.

 B = readtable('test.dat'),'Delimiter','\t');
 c = regexp(B{9,:}, ' ','split');
 CL_vec = c{1,1};
 CL_cell = CL_vec(13);
 Wing_CL = str2num(CL_cell{1,1});