MATLAB: Extract 2 data(temperature) from string

dataextractnumberstring

Code is
Str = [' <data seq="0" <temp8.0</temp <data seq="1" <temp6.9</temp '];
Str(strfind(Str, '>')) = [];
Key_1 = '<temp';
Index_1 = strfind(Str, Key_1);
Value_1 = sscanf(Str(Index_1 + length(Key_1):end),'%f');
But this code express in workspace
Value_1 = 8
I want to express in workspace
Value_1 = 8
Value_2 = 6.9
How can I make code ?

Best Answer

Use regexp to match strings which
  • follow after the string "<temp"
  • consist of (digit,period,digit)
  • are followed by "</temp"
str = [' <data seq="0" <temp8.0</temp <data seq="1" <temp6.9</temp '];
cac = regexp( str, '(?<=<temp)\d\.\d(?=</temp)', 'match' );
temp_2 = str2double(cac{2})
temp_1 = str2double(cac{1})
outputs
temp_2 =
6.9000
temp_1 =
8
>>
Or use sscanf. The format_string is a copy of Str, in which the "numbers" you want to extract are replaced by the specifier, %f
num = sscanf( Str, '<data seq="0" <temp%f</temp <data seq="1" <temp%f</temp' )
which outputs
num =
8.0000
6.9000
>>