Extracting double from endless serial string

2 visualizaciones (últimos 30 días)
Marcel Glomp
Marcel Glomp el 31 de En. de 2019
Comentada: Marcel Glomp el 5 de Feb. de 2019
Hey guys,
I am using fscanf to read from a serial port. The data I can read looks like this:
'2.304X+ 102.304X+ 102.304X+ 102.304X+ 1'
or
'.225X+ 1.225X+ 1.225X+ 1.225X+ 1.2'
All I want to do is to extract the 102.304 or the 1.225 as a double. The message has 7 characters
Right now I am doing it like this:
Live = fscanf(Serial,'%s',19); % Scanne den GMS
Value = extractBetween(Live,'+','X'); % Extrahiere die Zahl aus String
Final_Value(y,1) = str2double(Value);
I read 19 digits, so I can be sure to get the whole message.
It works so far, but is there a way to extract the double in a more elegant way?
Would be super nice if someone could help me!

Respuesta aceptada

David Goodmanson
David Goodmanson el 1 de Feb. de 2019
Hi Marcel,
Here is another way. It produces one copy of the number whereas in your case str2double(Value) produces a 3x1 matrix before it is assigned to Final_Value(y,1) in some manner. So there is that difference. And similar to what you are doing, you have to read in enough to be assured of two + signs.
Live = '2.304X+ 102.304X+ 102.304X+ 102.304X+ 1';
ind = find(Live == '+')
str2double(Live(ind(1)+1:ind(2)-2))

Más respuestas (1)

madhan ravi
madhan ravi el 2 de Feb. de 2019
str2double(regexp(Live,'(?<=+.*)(\d+)?.?\d*(?=X)','match')).'

Categorías

Más información sobre String Parsing en Help Center y File Exchange.

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by