Main Content


(To be removed) Write text to instrument, and read data from instrument

This serial, Bluetooth, tcpip, udp, visa, and gpib object function will be removed in a future release. Use serialport, bluetooth, tcpclient, tcpserver, udpport, and visadev object functions instead. For more information on updating your code, see Compatibility Considerations.


out = query(obj,'cmd')
out = query(obj,'cmd','wformat')
out = query(obj,'cmd','wformat','rformat')
[out,count] = query(...)
[out,count,msg] = query(...)
[out,count,msg,datagramaddress,datagramport] = query(...)



An interface object.


String that is written to the instrument.


Format for written data.


Format for read data.


Contains data read from the instrument.


The number of values read.


A message indicating if the read operation was unsuccessful.


The datagram address.


The datagram port.


out = query(obj,'cmd') writes the string cmd to the instrument connected to obj. The data read from the instrument is returned to out. By default, the %s\n format is used for cmd, and the %c format is used for the returned data.

out = query(obj,'cmd','wformat') writes the string cmd using the format specified by wformat.

wformat 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.

out = query(obj,'cmd','wformat','rformat') writes the string cmd using the format specified by wformat. The data read from the instrument is returned to out using the format specified by rformat.

rformat is a C language conversion specification. The supported conversion specifications are identical to those supported by wformat.

[out,count] = query(...) returns the number of values read to count.

[out,count,msg] = query(...) returns a warning message to msg if the read operation did not complete successfully.

[out,count,msg,datagramaddress,datagramport] = query(...) returns the remote address and port from which the datagram originated. These values are returned only when using a UDP object.


This example creates the GPIB object g, connects g to a Tektronix® TDS 210 oscilloscope, writes and reads text data using query, and then disconnects g from the instrument.

g = gpib('ni',0,1);
idn = query(g,'*IDN?')
idn =
TEKTRONIX,TDS 210,0,CF:91.1CT FV:v1.16 TDS2CM:CMV:v1.04


Before you can write or read data, obj must be connected to the instrument 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 query operation while obj is not connected to the instrument.

query operates only in synchronous mode, and blocks the command line until the write and read operations complete execution.

Using query is equivalent to using the fprintf and fgets functions. The rules for completing a write operation are described in the fprintf reference pages. The rules for completing a read operation are described in the fgets reference pages.


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.

Version History

Introduced before R2006a

expand all

R2021b: gpib object interface will be removed

Use of this function with a gpib object will be removed. To access a GPIB instrument, use the VISA-GPIB interface with a visadev object, its functions, and its properties instead.

The recommended functionality has additional capabilities and improved performance. See Transition Your Code to VISA-GPIB Interface for more information about using the recommended functionality.

See Also