LoginSignup
2
0

More than 5 years have passed since last update.

MATLABで複数バイト文字をchar型に分解,再構成

Posted at

シリアル通信とかでデータを投げたいときにfloatを1バイト文字に分解して送るのはよくあるかと思います。

matlabではtypecastという関数がこれに対応しています。
例えば3.34という少数を4バイトの文字列として送る際には,

>> typecast(single(3.34),'uint8')

ans =

  143  194   85   64

とこのようにこれらの4文字を送ればいいわけです。

なお,maltabはなるべくでかい型ですべての文字を保存しようとするのでsingle型に最初にキャストする必要があることには注意してください。

以下はだめな例です。

>> typecast([143  194   85   64],'single')

ans =

         0    3.5293         0    3.6289         0    3.3320         0    3.2500

きちんとuint8にキャストしてから関数にぶち込みましょう。

>> typecast(uint8([143  194   85   64]),'single')

ans =

    3.3400

なお,これをdSPACEのRTIシステムにぶん投げたところ,RTIシステムではtypecastが使えないとのことでエラーになりました。憤死。

Cで書くのが正着かもしれない

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0