read data from serial port , change to number and plot it

17 次查看(过去 30 天)
I'm working on a project that uses serial port to receive data, save to a variable so that I can process it later. I used fscanf command to achieve it and receive a large string but not numbers :
x=fscanf(s) %s is serial port name
x=
58514
58954
59386
59799
60207
60683
...
44598
45193
45809
46431
OK %1024 elements
I expect an array with 1024 elements but all I get is a very large string with both numbers and characters inside it. My question is whether I can use any command instead of fscanf, or how I can get only numbers (double or integer, not char) from that string. Thanks a lot

回答(1 个)

María
María 2014-5-19
Try to read the data like this:
x=fgets(s)
It will give you just one line (once you read it, it will give you the following line). Then, you can use the function str2num() to convert the string to a number
y=str2num(x)
I hope this works for you

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by