0

I have a text file which is an output from the command line of a diff program. I now read the file using

fileID     = fopen('runInfo.out','r');
file_dump  = textscan(fileID, '%s', 'Delimiter', '\n'); 
% Find postion of Min Cp to mark field on interest
Row        = find(~cellfun('isempty',strfind(file_dump{1}, 'Minimum Viscous  Cp')));
fclose(fileID);

I now have the row from which I need to extract data. The format of the file from this location looks something like

.OPERv   c>  
 u/Uinf =   1.0050   v/Uinf =  -0.0029
 q/Uinf =   1.0050   Cp     =  -0.0100


.OPERv   c>  
 u/Uinf =   1.0088   v/Uinf =  -0.0075
 q/Uinf =   1.0088   Cp     =  -0.0177


.OPERv   c>  
 u/Uinf =   1.0156   v/Uinf =  -0.0281
 q/Uinf =   1.0160   Cp     =  -0.0323

Since I already have this data in my cellArray from textscan, What I could think of was (pretty non robust)

u_line = vertcat(file_dump{1,1}...
         {find(~cellfun('isempty',strfind(file_dump{1}, 'u/Uinf'))),1})
v   = str2num(u_line(:,end-5:end));

and then somehow extract the numbers from these returned cells? In the end, I need the four values of u/Uinf, v/Uinf, q/Uinf and Cp. Is there a simpler option that I am missing?

Ash
  • 318
  • 3
  • 14
  • Is the text you posted in the second box (`u/Uinf = 1.0050 v/Uinf = -0.0029`) in the input text file you wnt to read? Which data do you want to read? Only the ones with `Uinf` tag? – il_raffa May 13 '15 at 13:14

1 Answers1

0

If the text file from which to extract the data is the one you posted in the sedond box, you can use a AWK script.

The followng AWK script reads the text file and generates as output a MatLab .m file containing the values extracted form the text file as arrays.

AWK script

BEGIN {
u_cnt=1;
v_cnt=1;
c_cnt=1;
q_cnt=1;
}
{
if($1 == "u/Uinf")
   {
      u_Uinf[u_cnt]=$3
      u_cnt++
   }  
if($4 == "v/Uinf")
   {
      v_Uinf[v_cnt]=$6
      v_cnt++
   }
if($4 == "Cp")
   {
      cp[c_cnt]=$6
      c_cnt++
   }
if($1 == "q/Uinf")
   {
      q_Uinf[q_cnt]=$3
      q_cnt++
   }
}
END {
print "u_Uinf_data=[" > "txt_file_data.m"
for(i=1;i<u_cnt;i++)
   print u_Uinf[i] > "txt_file_data.m"
print "];" > "txt_file_data.m"

print "v_Uinf_data=[" > "txt_file_data.m"
for(i=1;i<v_cnt;i++)
   print v_Uinf[i] > "txt_file_data.m"
print "];" > "txt_file_data.m"

print "q_Uinf_data=[" > "txt_file_data.m"
for(i=1;i<q_cnt;i++)
   print q_Uinf[i] > "txt_file_data.m"
print "];" > "txt_file_data.m"


print "cp_data=[" > "txt_file_data.m"
for(i=1;i<c_cnt;i++)
   print cp[i] > "txt_file_data.m"
print "];" > "txt_file_data.m"
}

Output .m file

u_Uinf_data=[
1.0050
1.0088
1.0156
];
v_Uinf_data=[
-0.0029
-0.0075
-0.0281
];
q_Uinf_data=[
1.0050
1.0088
1.0160
];
cp_data=[
-0.0100
-0.0177
-0.0323
];
il_raffa
  • 5,090
  • 129
  • 31
  • 36