fscanf() is producing inconsistent results
Show older comments
Hello, I am at a real tough spot. My senior design challenge is a data acquisition system and I am using matlab as to collect data from Arduino. The code is mostly done, but I am having very poor results using fscanf() to collect a string of data.
My Arduino is sending the following string of 2 floating values through the serial communication port.
832.5767 22.5783
My code I am using is the following:
if true
while n < 10000
timer = toc;
if timer >= round(timer*10)/10 && clone ~= round(timer*10)/10;
clone = round(timer*10)/10;
Time(n) = round(timer*10)/10; % writes data in time matrix.
% Begin data acquisition.
input = fscanf(s)
input = cell2mat(textscan(input, '%f %f'))
n = n + 1;
end
pause(0.00001) % Pause is essential to break loop.
end
end
Everything works beautifully until about a minute and then fscanf() starts producing inconsistent reads. Changing the baud rate has no effect. Its like clockwork where errors begin to occur at the 1400ish iteration.

Please help me, my grade is dependent on the success of this project.
Accepted Answer
More Answers (1)
Jan
on 7 Apr 2014
Your loop wastes a lot of time. Why do you start about 1000 pause commands instead of waiting for 10 seconds? What about pause(10) to wait once only?
A timer callback would be even nicer.
This looks easier than the indirection over textscan and cell2mat:
input = fscanf(s, '%f %f')
Your problem seems to be, that the external device does not send the data fast enough. It looks like "832.5767 22" has been sent while your program reads and in the next iteration you get the string ".5783" only. Better think of another method to detect the availability of data. Which line break did you specify in the fopen command to open the serial connection?
Look for "BytesAvailable" and further hints on http://www.mathworks.com/help/matlab/ref/serial.fscanf.html .
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!