MATLAB 从具有混合字符串、整数和间距的 DAT 文件中获取浮点数
MATLAB Getting a floating point out of a DAT files with mixed strings, integers and spacing
我正在使用 MATLAB 中的外部软件。这个软件给了我一个输出.dat 文件,我想从中提取一段信息。
.dat 文件包含以下内容:
---------------------------------------------------------------
Vortex Lattice Output -- Total Forces
Configuration: Fokker F27 FRIENDSHIP
# Surfaces = 2
# Strips = 40
# Vortices = 320
Sref = 35.000 Cref = 2.4325 Bref = 14.500
Xref = 18.531 Yref = 0.0000 Zref =-0.70925
Standard axis orientation, X fwd, Z down
Run case: -unnamed-
Alpha = 3.93282 pb/2V = -0.00000 p'b/2V = -0.00000
Beta = 0.00000 qc/2V = 0.00000
Mach = 0.274 rb/2V = -0.00000 r'b/2V = -0.00000
CXtot = 0.06409 Cltot = -0.00000 Cl'tot = 0.00000
CYtot = -0.00000 Cmtot = 8.27673
CZtot = -1.19843 Cntot = 0.00000 Cn'tot = 0.00000
CLtot = 1.20000
CDtot = 0.01826
CDvis = 0.00000 CDind = 0.01826
CLff = 1.20138 CDff = 0.01945 | Trefftz
CYff = -0.00000 e = 3.9315 | Plane
---------------------------------------------------------------
我想提取 Alpha 字符串旁边的数值,在本例中为 3.93。输出文件始终具有相同的布局,因此 Alpha 字符串始终位于相同的位置,其值只会发生变化。
我已经尝试将 fopen
与 textscan
和 fscanf
一起使用,但我无法从文件中获取数据。
有没有办法在 MATLAB 中自动执行此操作?
对于那些没有表格的人来说,这是一个解决方案,因为这些是在 MATLAB R2013b 中引入的。
我逐行手动解析文件,找到了包含 Alpha
字符串的行。找到它后,我删除了这一行的所有空格,然后使用正则表达式查找以下模式:
[a-zA-Z]+=-*[0-9]*\.*[0-9]*
此模式搜索:
Alpha
- ...后接等号
- ...后跟一个可能的破折号表示负值
- ...后跟可能的数字序列
- ...后跟一个可能的句点
- ...后跟另一个可能的数字序列
如果你去掉所有的空格,你只会得到一个你想要的输出匹配:
Alpha=3.93282
只需提取第一个标记,然后找到 Alpha=
字符串并将其删除。最后,将所需的数字从字符串转换为数字。
代码如下:
%// Open up the file
f = fopen('test.dat');
%// Go through the file...
while true
%// Get a line
line = fgetl(f);
%// Have we reached the end of the file? Get out then
if line == -1
break
end
%// If we find a line with the Alpha string in it...
if ~isempty(strfind(line, 'Alpha'))
%// Strip out all spaces
line = strrep(line, ' ', '');
%// Find the pattern discussed above
cel = regexp(line, 'Alpha+=-*[0-9]*\.*[0-9]*', 'match');
%// Remove the Alpha= string
cel = strrep(cel, 'Alpha=', '');
%// Convert the string value into a numeric value and store
%// this in a variable called value
value = str2double(cel);
%// Get out of the loop
break;
end
end
%// Close the file
fclose(f);
我正在使用 MATLAB 中的外部软件。这个软件给了我一个输出.dat 文件,我想从中提取一段信息。
.dat 文件包含以下内容:
---------------------------------------------------------------
Vortex Lattice Output -- Total Forces
Configuration: Fokker F27 FRIENDSHIP
# Surfaces = 2
# Strips = 40
# Vortices = 320
Sref = 35.000 Cref = 2.4325 Bref = 14.500
Xref = 18.531 Yref = 0.0000 Zref =-0.70925
Standard axis orientation, X fwd, Z down
Run case: -unnamed-
Alpha = 3.93282 pb/2V = -0.00000 p'b/2V = -0.00000
Beta = 0.00000 qc/2V = 0.00000
Mach = 0.274 rb/2V = -0.00000 r'b/2V = -0.00000
CXtot = 0.06409 Cltot = -0.00000 Cl'tot = 0.00000
CYtot = -0.00000 Cmtot = 8.27673
CZtot = -1.19843 Cntot = 0.00000 Cn'tot = 0.00000
CLtot = 1.20000
CDtot = 0.01826
CDvis = 0.00000 CDind = 0.01826
CLff = 1.20138 CDff = 0.01945 | Trefftz
CYff = -0.00000 e = 3.9315 | Plane
---------------------------------------------------------------
我想提取 Alpha 字符串旁边的数值,在本例中为 3.93。输出文件始终具有相同的布局,因此 Alpha 字符串始终位于相同的位置,其值只会发生变化。
我已经尝试将 fopen
与 textscan
和 fscanf
一起使用,但我无法从文件中获取数据。
有没有办法在 MATLAB 中自动执行此操作?
对于那些没有表格的人来说,这是一个解决方案,因为这些是在 MATLAB R2013b 中引入的。
我逐行手动解析文件,找到了包含 Alpha
字符串的行。找到它后,我删除了这一行的所有空格,然后使用正则表达式查找以下模式:
[a-zA-Z]+=-*[0-9]*\.*[0-9]*
此模式搜索:
Alpha
- ...后接等号
- ...后跟一个可能的破折号表示负值
- ...后跟可能的数字序列
- ...后跟一个可能的句点
- ...后跟另一个可能的数字序列
如果你去掉所有的空格,你只会得到一个你想要的输出匹配:
Alpha=3.93282
只需提取第一个标记,然后找到 Alpha=
字符串并将其删除。最后,将所需的数字从字符串转换为数字。
代码如下:
%// Open up the file
f = fopen('test.dat');
%// Go through the file...
while true
%// Get a line
line = fgetl(f);
%// Have we reached the end of the file? Get out then
if line == -1
break
end
%// If we find a line with the Alpha string in it...
if ~isempty(strfind(line, 'Alpha'))
%// Strip out all spaces
line = strrep(line, ' ', '');
%// Find the pattern discussed above
cel = regexp(line, 'Alpha+=-*[0-9]*\.*[0-9]*', 'match');
%// Remove the Alpha= string
cel = strrep(cel, 'Alpha=', '');
%// Convert the string value into a numeric value and store
%// this in a variable called value
value = str2double(cel);
%// Get out of the loop
break;
end
end
%// Close the file
fclose(f);