Write text to instrument
An interface object.
The string written to the instrument.
C language conversion specification.
Specifies whether data is written synchronously or asynchronously.
fprintf(obj,'cmd') writes the string
cmd to the instrument connected to
The default format is
%s\n. The write operation is synchronous
and blocks the command line until execution is complete.
writes the string using the format specified by
format is a C language conversion specification.
Conversion specifications involve the
% character and the
conversion characters d, i, o, u, x, X, f, e, E, g, G, c, and s. Refer to the
sprintf file I/O format
specifications or a C manual for more information.
writes the string with command-line access specified by
cmd is written synchronously and the
command line is blocked. If
cmd is written asynchronously and
the command line is not blocked. If
mode is not
specified, the write operation is synchronous.
writes the string using the specified format. If
cmd is written synchronously. If
cmd is written asynchronously.
Create the serial port object
on a Windows® machine to a Tektronix® TDS 210 oscilloscope, and write the
RS232? instructs the scope to return serial port
s = serial('COM1'); fopen(s) fprintf(s,'RS232?') settings = fscanf(s) settings = 9600;1;0;NONE;LF
Because the default format for
%s\n, the terminator specified by the
Terminator property was automatically written. However, in
some cases you might want to suppress writing the terminator. To do so, you must
explicitly specify a format for the data that does not include the terminator, or
configure the terminator to empty.
Before you can write text to the instrument, it must be connected to
obj with the
fopen function. A connected
interface object has a
Status property value of
open. An error is returned if you attempt to perform a write
obj is not connected to the instrument.
ValuesSent property value is increased by the number of
values written each time
fprintf is issued.
An error occurs if the output buffer cannot hold all the data to be written. You
can specify the size of the output buffer with the
fprintf function will return an error message if you set the
flowcontrol property to
hardware on a
serial object, and a hardware connection is not detected. This occurs if a device is
not connected, or a connected device is not asserting that is ready to receive data.
Check you remote device's status and flow control settings to see if hardware flow
control is causing errors in MATLAB®.
If you want to check to see if the device is asserting that it is ready to
receive data, set the
none. Once you connect to the device check the
PinStatus structure for
off, there is a problem
on the remote device side. If
on, there is a hardware
device prepared to receive data and you can execute
To get a list of options you can use on a function, press the Tab key after entering a function on the MATLAB command line. The list expands, and you can scroll to choose a property or value. For information about using this advanced tab completion feature, see Using Tab Completion for Functions.
By default, text is written to the instrument synchronously and the command
line is blocked until the operation completes. You can perform an asynchronous
write by configuring the
mode input argument to be
async. For asynchronous writes,
BytesToOutput property value is continuously
updated to reflect the number of bytes in the output buffer.
The callback function specified for the
OutputEmptyFcn property is executed when the
output buffer is empty.
You can determine whether an asynchronous write operation is in progress with
Synchronous and asynchronous write operations are discussed in more detail in Synchronous Versus Asynchronous Write Operations.
A write operation using
fprintf completes when
The specified data is written.
The time specified by the
For serial port, TCPIP, UDP, and VISA-serial objects, all occurrences of
cmd are replaced with the
Terminator property value. Therefore, when using the
%s\n, all commands written to the instrument
will end with this property value.
For GPIB, VISA-GPIB, VISA-VXI, and VISA-GPIB-VXI objects, all occurrences of
cmd are replaced with the
EOSCharCode property value if the
EOSMode property is set to
read&write. For example, if
EOSMode is set to
EOSCharCode is set to
LF, then all
\n are replaced with a line feed character.
Additionally, for GPIB objects, the End Or Identify (EOI) line is asserted when
the terminator is written out.
The terminator required by your instrument will be described in its documentation.
serialobject interface is not recommended
Not recommended starting in R2019b