Reading numeric value from COM port

I try to use fscanf( ) to get the numeric heading of an electronic compass. The compass is sending data through COM1 with NMEA standard. When I open the port, and use FSCANF(ser) I get
ans =
$HCHDG,49.0,,,,
My problem is: how to filter the 49.0 out of this string, and convert it to a numeric value (eg float) so I can use it for calculations?

답변 (1개)

Walter Roberson
Walter Roberson 2011년 10월 15일

0 개 추천

textscan(). Or fscanf() or sscanf(). Or strfind() and index to extract the number as a string and str2double() it. Or regexp() to extract the number as a string and str2double() it.
A format of '%*7c' would eat the '$HCHDG,' and discard it.

댓글 수: 2

Jan De Vries
Jan De Vries 2011년 10월 15일
Thanks! It works with the str2double() function.
Is this also possible in simulink?
Walter Roberson
Walter Roberson 2011년 10월 15일
Sorry, I am not familiar enough with simulink. I am sure there is _some_ way, but I do not know if there is a "nice" way.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

질문:

2011년 10월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by