User`s manual

8 Serial Port I/O
8-44
delete(s)
clear s
Example: Parsing Input Data Using strread
This example illustrates how to use the strread function to parse and format
data that you read from a device.
strread is particularly useful when you want
to parse a string into one or more variables, where each variable has its own
specified format.
The instrument is a Tektronix TDS 210 two-channel oscilloscope connected to
the serial port COM1.
1. Create a serial port object – Create the serial port object s associated with
serial port COM1.
s = serial('COM1');
2. Connect to the device – Connect s to the oscilloscope. Since the default
value for the
ReadAsyncMode property is continuous, data is asynchronously
returned to the input buffer as soon as it is available from the instrument.
fopen(s)
3. Write and read data – Write the RS232? command to the instrument using
fprintf, and then read back the result of the command using fscanf. RS232?
queries the RS-232 settings and returns the baud rate, the software flow
control setting, the hardware flow control setting, the parity type, and the
terminator.
fprintf(s,'RS232?')
data = fscanf(s)
data =
9600;0;0;NONE;LF
Use the strread function to parse and format the data variable into five new
variables.
[br,sfc,hfc,par,tm] = strread(data,'%d%d%d%s%s','delimiter',';')
br =
9600
sfc =
0