matlab读取格式重复,如何在MATLAB中从重复的大文本文件格式读取数据?

icon1.gif 如何在MATLAB中从重复的大文本文件格式读取数据?

我想读取水的平均饱和度(%)数据,如下所示。此数据是大文件的部分形式,但是平均水饱和度(%)仅以给定格式重复。

Average Pressure Total Pore Volume psia 3884.9 HC. Pore Volume psia 3884.9 Average P/Z Total Pore Volume psia 4457.8 HC. Pore Volume psia 4457.8 Average Saturation % Oil 84.911 Gas .08873 Water 15.000 Percentage Recovery Stock Tank Oil .02211 STO as a % of Mobile Oil .02891 Total Gas .02034 Water 62e-12 我试图通过使用readline.m函数来执行此操作,但是不幸的是,平均水饱和度(%)数据的位置不是由行号固定的。对于不同型号的同类输出文件,行号会更改。

这就是我想要做的:

%# Reading Water Saturation (Sw) data from output (.OUT) file of reservoir model Sw_LineNo=[554,968,1120,1272,1424,1576,1728,1880,2032,2184,2336,2488,2640,2792,2944,3096,3248,3400,3552,3704,3856]; % This column vector contains the line numbers of the .out file with Sw values for year 1 till 20 for i=1:size(Sw_LineNo,2) read_value=readline('ReservoirModel_ExplorWell_CMGBuilder.out',Sw_LineNo(i)); % read_value stores values in form of string Swav_Data_E_W(i,j)=str2num(read_value(33:38)); % converts the required portion of string (Sw value) to number end 现在,如果我的模型( ReservoirModel_ExplorWell_CMGBuilder.out )发生了变化,则文本文件中水的平均饱和度(%)所在的行号也会发生变化。因此Sw_LineNo对于不同的模型Sw_LineNo变化,并且我有大量的模型。

请提出正确的方法来读取水数据的所有平均饱和度(%)。

回答:

%# Reading Average Water Saturation (Savw) data from output (.OUT) file of reservoir model fid = fopen('ReservoirModel_CMGBuilder.out'); % open the file dotOUT_fileContents = textscan(fid,'%s','Delimiter','\n'); % read it into one big array, row by row dotOUT_fileContents = dotOUT_fileContents{1}; fclose(fid); %# don't forget to close the file again %# find rows containing 'Average Saturation %' Swav_Starts = strmatch('Average Saturation %',dotOUT_fileContents); % Swav_Starts contains the line numbers wherever 'Average Saturation %' is found nSwav = length(Swav_Starts); % total no. of Swav values will be equal to the total no. of 'Average Saturation %' read from the .out file %# loop through the file and read the numeric data for w = 1:nSwav %# read lines containing numbers tmp_str = dotOUT_fileContents(Swav_Starts(w)+3); % stores the content of the 3rd row from the row containing 'Average Saturation %' in form of string tmp_str = tmp_str{:}; % store the content of the string which contains Swav, as well, in form of a character %# assign output Swav_yearly(w,j) = str2num(tmp_str(30:35)); % convert the part of the character containing Swav into number end 现在tmp_str = dotOUT_fileContents(Swav_Starts(w)+3);产生以下字符串:

Water 15.000 如果尝试使用str2num将其转换为数字,则会得到一个空矩阵。因此,我选择了包含饱和度值(此处为15.000 )的此字符串的字符,然后将该字符更改为数字,如下所示,给出了平均水饱和度的值:

str2num(tmp_str(30:35)) 请告知有人是否有任何方法可以从字符串中提取数字而无需像我一样挑选字符。

更多&回答...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值