Filter löschen
Filter löschen

extract 2 data(temperature) from string

1 Ansicht (letzte 30 Tage)
Jeong_evolution
Jeong_evolution am 29 Okt. 2016
Bearbeitet: per isakson am 29 Okt. 2016
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 ?

Akzeptierte Antwort

per isakson
per isakson am 29 Okt. 2016
Bearbeitet: per isakson am 29 Okt. 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 Kommentar
dpb
dpb am 29 Okt. 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
>>

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

dpb
dpb am 29 Okt. 2016
Bearbeitet: dpb am 29 Okt. 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.

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by