Azzera filtri
Azzera filtri

extract 2 data(temperature) from string

1 visualizzazione (ultimi 30 giorni)
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 ?

Risposta accettata

per isakson
per isakson il 29 Ott 2016
Modificato: per isakson il 29 Ott 2016
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
>>
  1 Commento
dpb
dpb il 29 Ott 2016
Good...somebody that does know regular expressions... :) I'd note for OP that can wrap the above in str2double and avoid the named variable issues cleanly...
>> str2double(regexp( Str, '(?<=<temp)\d\.\d(?=</temp)', 'match' ))
ans =
8.0000 6.9000
>>

Accedi per commentare.

Più risposte (1)

dpb
dpb il 29 Ott 2016
Modificato: dpb il 29 Ott 2016
This would be a good place for regular expressions, but I'm a dweeb when it comes to trying to write the proper parsing expression...with string operations, I'd do this something like--
>> Str = [' <data seq="0" <temp8.0</temp <data seq="1" <temp6.9</temp '];
>> t=tokens(Str,'<');
>> Values=str2num(t(t(:,1)=='t',5:end))
Values =
8.0000
6.9000
>>
The above takes advantage that the string 'temp' trailing the value is returned with the leading '/' so the first character in the resulting array of tokens being 't' identifies the desired rows. Then, since it's a fixed-length string of four leading characters, simply return the remainder of the string '5:end' and convert to numeric.
tokens is my little utility routine--
>> type tokens
function tok = tokens(s,d)
% Simple string parser returns tokens in input string s
%
% T=TOKENS(S) returns the tokens in the string S delimited
% by "white space". Any leading white space characters are ignored.
%
% TOKENS(S,D) returns tokens delimited by one of the
% characters in D. Any leading delimiter characters are ignored.
% Get initial token and set up for rest
if nargin==1
[tok,r] = strtok(s);
while ~isempty(r)
[t,r] = strtok(r);
tok = strvcat(tok,t);
end
else
[tok,r] = strtok(s,d);
while ~isempty(r)
[t,r] = strtok(r,d);
tok = strvcat(tok,t);
end
end
>>
NB: It's a very bad idea to "poof" variables into the workspace with names such as you've written; it leads to requiring eval to process them later and that just leads to a location where "there be dragons" and is to be avoided. Use an array as shown instead.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by