How to convert ascii data to floating point?

9 ビュー (過去 30 日間)
YoungMin Ho
YoungMin Ho 2016 年 11 月 26 日
コメント済み: YoungMin Ho 2016 年 11 月 30 日
Hi guys.
i have a problem with my project and i need your help.
the problem is about data type conversion.
i use an app to send gravity_sensor data via UDP on my phone.
This is a desription of the app i use.
%%%%%%%%%%%%%
USAGE INSTRUCTIONS:
The data is stored within the app as a float array. See the format below.
Format: [ X Acceleration, Y Acceleration, Z Acceleration, X Gravity, Y Gravity, Z Gravity, X Rotation Rate, Y Rotation Rate, Z Rotation Rate, X Orientation (Azimuth), Y Orientation (Pitch), Z Orientation (Roll), deprecated, deprecated, Ambient Light, Proximity, Keyboard Buttons 1 - 8 ]
%%%%%%%%%%%%%
and, i wrote these lines and get result in MATLAB.
fopen(UDPComIn);
csvdata=fscanf(UDPComIn)
ans =
KKun
this is the problem. what i wanted was floating point data like (-0.001). but, the result data type looked like ASCII code type.
so here are my question.
1) Is there any method to extract floating point data from a float array in UDP ?
2) if not, how can i convert ascii data to floating point?
i will wait your reply soon

採用された回答

Walter Roberson
Walter Roberson 2016 年 11 月 26 日
The default for fscanf on UDP objects is %c format, but the packets do not contain text. You should be using fread() on the UDP object, specifying '*float' . You might also need to specify the endian order; see https://www.mathworks.com/help/instrument/byteorder.html
  1 件のコメント
YoungMin Ho
YoungMin Ho 2016 年 11 月 30 日
it works. thanks!

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2016 年 11 月 26 日
That won't work. If you passed UDPComIn into fopen(), then it's a string with a filename in it. So you can't pass that in to fscanf. Anyway, you didn't even pass a file ID or a format specifier string into fscanf(). Check the documentation for fscanf(). Or use str2double() instead, if you can get your numbers in an ASCII string.
  1 件のコメント
Walter Roberson
Walter Roberson 2016 年 11 月 26 日
"If you passed UDPComIn into fopen(), then it's a string with a filename in it."
That is not correct. You can fopen() on Serial objects and TCP objects and UDP objects.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by