Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

read

Read data from remote host over TCP/IP

Syntax

read(t)
read(t,size)
read(t,size,datatype)

Description

example

read(t) reads all available bytes of data from tcpclient object t connected to the remote host and returns the data. The number of values read is determined by the BytesAvailable property.

For any read or write operation, the data type is converted to uint8 for the data transfer. It is then converted back to whatever data type you set if you specified another data type.

example

read(t,size) reads the specified number of values, size, from tcpclient object t connected to the remote host and returns the data. If size is greater than the object's BytesAvailable property, then the function waits until the specified amount of data is read or the timeout is reached.

example

read(t,size,datatype) reads the specified number of values, size, with the specified precision, datatype, from tcpclient object t connected to the remote host and returns the data. The datatype argument is a character vector of a standard MATLAB® data type.

For any read or write operation, the data type is converted to uint8 for the data transfer. It is then converted back to whatever data type you set if you specified another data type.

Examples

collapse all

Create a TCP/IP object called t, using the IP address shown, and Port of 4012.

t = tcpclient('172.28.154.231', 4012)
t = 

   tcpclient with properties:

          Address: '172.28.154.231'
             Port: 4012
          Timeout: 10
   BytesAvailable: 0

Read all the bytes of data available.

read(t)

The read function used with no arguments reads all available bytes of data from tcpclient object t connected to the remote host and returns the data. The number of values read is determined by the BytesAvailable property, which is equal to the numbers of bytes available in the input buffer.

Close the connection between the TCP/IP client object and the remote host by clearing the object.

clear t

Create a TCP/IP object called t, connecting to a TCP/IP echo server, with Port of 7.

t = tcpclient('localhost', 7)
t = 

   tcpclient with properties:

          Address: 'local host'
             Port: 7
          Timeout: 10
   BytesAvailable: 0

Assign 10 bytes of data to the variable data.

data = (1:10)

Check the data.

whos data
Name     Size     Bytes     Class     Attributes

data     1x10        10     double

Write data to the echo server.

write(t, data)

Check that the data was written using the BytesAvailable property.

t.BytesAvailable
ans = 

    80

For any read or write operation, the data type is converted to uint8 for the data transfer. It is then converted back to whatever data type you set if you specified another data type. Since 1 double equals 8 uint8 bytes, there are 80 bytes available.

Read 10 doubles from the server. The object name is always the first argument. The size argument must be the second argument, and datatype must be the third argument.

read(t, 10, 'double')
ans = 

  1    2    3    4    5    6    7    8    9    10

Close the connection between the TCP/IP client object and the remote host by clearing the object.

clear t

Input Arguments

collapse all

Number of bytes to read, specified as a numeric scalar. Size cannot be set to inf. If size is greater than the object's BytesAvailable property, the function waits until the specified amount of data is read. The first argument must be the object name, and the second argument is the size. The size argument is optional.

Example: read(t, 5)

Data Types: double

MATLAB data type, specified as a character vector. Size cannot be set to inf. The datatype must be set to one of the 10 values shown above. The first argument must be the object name, the second argument is the size, and the third argument is the data type. The size and datatype arguments are optional.

For any read or write operation, the data type is converted to uint8 for the data transfer. It is then converted back to whatever data type you set if you specified another data type.

Example: read(t, 10, 'double')

Data Types: char

Introduced in R2014b

Was this topic helpful?