| Products & Services | Industries | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
fprintf(obj,'cmd')
fprintf(obj,'format','cmd')
fprintf(obj,'cmd','mode')
fprintf(obj,'format','cmd','mode')
fprintf(obj,'cmd') writes the string cmd to the device connected to the serial port object, obj. The default format is %s\n. The write operation is synchronous and blocks the command-line until execution completes.
fprintf(obj,'format','cmd') writes the string using the format specified by format.
fprintf(obj,'cmd','mode') writes the string with command line access specified by mode. mode specifies if cmd is written synchronously or asynchronously.
fprintf(obj,'format','cmd','mode') writes the string using the specified format. format is a C language conversion specification.
You need an open connection from the serial port object, obj, to the device before performing read or write operations.
Use the fopen function to open a connection to the device. When obj has an open connection to the device it has a Status property value of open. Refer to Troubleshooting Common Errors for fprintf errors.
To understand the use of fprintf refer to Completing a Write Operation with fprintf and Rules for Writing the Terminator.
format |
ANSI C conversion specification includes these conversion characters.
| ||||||||||||||||||||||||||
mode |
Specifies whether the string cmd is written synchronously or asynchronously:
If mode is not specified, the write operation is synchronous. If you specify asynchronous mode, when the write operation occurs:
Use the TransferStatus property to determine whether an asynchronous write operation is in progress. For more information on synchronous and asynchronous write operations, see Controlling Access to the MATLAB Command Line. |
Create a serial port object s and connect it to a Tektronix TDS 210 oscilloscope. Write the RS232? command with fprintf. RS232? instructs the scope to return serial port communications settings. This example works on a Windows platform.
s = serial('COM1');
fopen(s)
fprintf(s,'RS232?')
Specify a format for the data that does not include the terminator, or configure the terminator to empty.
s = serial('COM1');
fopen(s)
fprintf(s,'%s','RS232?')
The default format for fprintf is %s\n. Therefore, the terminator specified by the Terminator property is automatically written. However, in some cases you might want to suppress writing the terminator.
Specify an array of formats and commands:
s = serial('COM1');
fopen(s)
fprintf(s,['ch:%d scale:%d'],[1 20e-3],'sync');
BytesToOutput | fopen | fwrite | OutputBufferSize | OutputEmptyFcn | Status | stopasync | TransferStatus | ValuesSent
![]() | fprintf | frame2im | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |