MATLAB: I would to save the values for post processing from file output, can you help me

importing data into variablesMATLABreadregexptext file

I have a file .txt which i would to obtain the values for post processing. For this in Matlab i am using:
outputfile=fopen(postproces.dat,'r');
But the file have this line:
fan= 30.000 bes = 2.3700 doe = 15.490
and i would to catch 2.3700 and save it into a variabile bes in Matlab. Thanks.

Best Answer

Try this
>> num = cssm('h:\m\cssm\postprocessing.txt')
num =
3.15
where the file, cssm.m, contains the following code
function num = cssm( ffs )
str = fileread( ffs );
xpr = '(?<=Cref\s*=\s*)[0-9.]+';
cac = regexp( str, xpr, 'match', 'once' );
num = str2double( cac );
end
2017-05-07: There is an issue with this regular expression, '(?<=Cref\s*=\s*)[0-9.]+'. It matches the value following any word that ends with the string, Cref, e.g. xyzCref. Thus, add the beginning of word anchor, \<, so that the expression reads '(?<=\<Cref\s*=\s*)[0-9.]+'.
Version 2.0
>> num = cssm('h:\m\cssm\postprocessing.txt',{'Sref','Cref','Bref'})
num =
30 3.15 10.95
where
function num = cssm( ffs, names )
narginchk(1,2)
if nargin == 1
names = {'Cref'};
else
if isa( names, 'char' )
names = {names};
elseif not( iscellstr( names ) )
error('The second input argument, "%s", has wrong type')
end
end
str = fileread( ffs );
len = length( names );
num = nan( size( names ) );
for jj = 1 : len
xpr = ['(?<=\<', names{jj}, '\s*=\s*)[0-9.]+'];
cac = regexp( str, xpr, 'match', 'once' );
num(jj) = str2double( cac );
end
end