fscanf ; multiple columns of data from arduino

2 views (last 30 days)
serot12 tt
serot12 tt on 10 Dec 2016
Commented: dpb on 11 Dec 2016
I have Arduino serial screen data like
ax ay az gx gy gz mx my mz
ax ay az gx gy gz mx my mz
ax ay az gx gy gz mx my mz
. . . . . . . . .
. . . . . . . . .
As it is like above I have different columns of data.
With fscanf command I want to collect these data column by column.
ax=fscanf(b,'%f');
ay=fscanf(b,'%d');
az=fscanf(b,'%d');
gx=fscanf(b,'%d');
gy=fscanf(b,'%d');
gz=fscanf(b,'%d');
mx=fscanf(b,'%d');
my=fscanf(b,'%d');
mz=fscanf(b,'%d');
(b is my fopen device)
With that code I can't get data column by column.I can just take one raw and it is mixed(like for ax it take first raw which includes ay az .... mz).
Have can I plot multiple columns of data?
  1 Comment
dpb
dpb on 11 Dec 2016
I've never used Arduino so have no info at all about communication with it but...
If you were to really want to read only a single value at a time (would seem very inefficient way to do it), then for each read operation you'd have to set a count of 1...like
ax=fscanf(b,'%f',1);
You'd then have to repeat this for each variable and then loop until all were done, keeping a loop index to store additional values to plot when acq is done.
If all the data are numeric as it appears from the formatting, then '%f' works for any type and, as you've discovered, you can return a series of readings in a single operation (bound to be far more efficient).
You can then just address the desired element in that array by column index and the colon ':' operator for rows such as
dat=fscanf(b,'%f');
x=dat(:,1); % first column
or just plot the results directly as Matlab will assume each column a separate variable automagically.
I'm sure there are many examples of returning data from Arduino if you'll do some more searching...

Sign in to comment.

Answers (0)

Categories

Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!