How to separate string data and convert it to number?

2 ビュー (過去 30 日間)
Setiawan Putra Hendratno
Setiawan Putra Hendratno 2017 年 2 月 28 日
コメント済み: Vandana Rajan 2017 年 3 月 1 日
I have a problem to separate this data BE1234U1234 which B is the begin of counting real time count for sensor, i.e. encoder motor and ultrasonic distance, E for encoder, and U for ultrasonic distance.
I'm using Altera to acquired data and read it to MATLAB. But I can only separate the data from B1234E for encoder only. How can I read and separate the data into 1234 for encoder and 1234 for ultasonic distance respectively?
Thank You,
Setiawan

回答 (2 件)

Stephen23
Stephen23 2017 年 2 月 28 日
編集済み: Stephen23 2017 年 2 月 28 日
>> str = 'BE1234U1234';
>> sscanf(str,'BE%dU%d')
ans =
1234
1234
for example:
>> str = 'BE1234U5678';
>> vec = sscanf(str,'BE%dU%d');
>> enc = vec(1)
enc = 1234
>> ult = vec(2)
ult = 5678
  1 件のコメント
Setiawan Putra Hendratno
Setiawan Putra Hendratno 2017 年 2 月 28 日
How if the data is modified to 1234-1234 in Altera?

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


Vandana Rajan
Vandana Rajan 2017 年 2 月 28 日
Hi,
' ismember ' function can help you here.
>> [e_val, e_pos] = ismember('E',c);
>> [u_val, u_pos] = ismember('U',c);
>> enc_val = c(e_pos+1:u_pos-1)
enc_val =
1234
>> ultra_val = c(u_pos+1:end)
ultra_val =
1234
  3 件のコメント
Setiawan Putra Hendratno
Setiawan Putra Hendratno 2017 年 2 月 28 日
At least I can separate the data. Thank you. I'll try it in my supervisor's lab.
Vandana Rajan
Vandana Rajan 2017 年 3 月 1 日
@Stephen Cobeldick,
Yeah. I missed that part. Thanks for mentioning that :)

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by