How to read serial data formatted in CSV form?

17 views (last 30 days)
So I've been spending quite some time on this and the coding for interpreting the data has been easy. Its just been the actual data acquisition process that has been troubling.
I have an Arduino Uno and its currently giving out its coordinates in the format of:
X, Y, Time (seconds)
So it looks like a constantly updated csv file, sending information through the serial port. My current acquisition code for what works is:
clear all;
close all;
delete(instrfindall);
try
% Initialize serial port
s = serial('COM3');
set(s,'BaudRate', 9600);
set(s,'DataBits', 8);
set(s,'StopBits', 1);
fopen(s);
s.ReadAsyncMode = 'continuous';
% Main graph figure
figure(1);
hold on;
title('Mouse Positioning');
xlabel('X-Position');
ylabel('Y-Posfopetion');
hold off
data(i) = fscanf(s, '%c')
end
However, I'm not sure how I would loop it so it would constantly acquire the data when I want it to(see below), and second of all, I know %c for fscan is wrong. Does matlab have a function to collect data in CSV format?
As for when to start and stop data acquisition, I was thinking of either having the arduino receive the signal, or have matlab communicate with another program. My lab is using a TDT system as our input and output for data acquisition. It can send information through BNST cables through a program called RPvdsEx. Is there a way to use TDT/RPvdsEx to trigger either the arduino with BNST cables or matlab to begin or stop the data acquisition?
Thanks in advance.

Answers (1)

Walter Roberson
Walter Roberson on 30 Aug 2011
MATLAB does not have a function to collect data in CSV format.
However, if you have a string S (which might potentially have linefeeds or carriage returns in it), such as data read from a serial device, then you can use
textscan(S, '%f%f%f', 'Delimiter', ',', 'CollectOutput', 1)
to read all the trios that are stored in the string S.
Usually the first argument to textscan() is a file identifier, but you can pass a string instead.
Also, it is permitted to pass a serial object in place of a file identifier so that textscan() would read directly from the serial device:
textscan(s, '%f%f%f', 'Delimiter', ',', 'CollectOutput', 1)
The disadvantage of that is unless you pass the number of lines to read, textscan is going to continue to try to read information until it encounters a response that cannot be interpreted in the format, or until it receives a device error.

Categories

Find more on Digital Input and Output in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!