Create a UDP Object

UDP Object

You create a UDP object with the udp function. udp does not require the name of the remote host as an input argument. However, if you are using the object to communicate with a specific instrument, you should specify the remote host and the port number.


Although UDP is a stateless connection, opening a UDP object with an invalid host name will generate an error.

As described in Configuring Properties During Object Creation, you can also configure property values during object creation, such as the LocalPort property if you will use the object to read data from the instrument.

For example, to create a UDP object associated with the remote host, remote port 4012, and local port 3533,

u = udp('', 4012, 'LocalPort', 3533);

The UDP object u now exists in the MATLAB® workspace. You can display the class of u with the whos command.

whos u
  Name      Size                   Bytes  Class

  u         1x1                      632  udp object

Grand total is 12 elements using 632 bytes

When the UDP object is created, the following properties are assigned values based on the values provided to the upd function. These general purpose properties provide information about the UDP object.

UDP Descriptive Properties

Property Name



Specify a descriptive name for the UDP object.


Specify the remote host.


Specify the remote host port for the connection.


Indicate the object type.


Specify the local host port, if you are expecting to receive data from the instrument.


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.

You can display the values of these properties for u with the get function.

ans = 
ans =
ans = 
ans = 
ans = 

The UDP Object Display

The UDP object provides you with a convenient display that summarizes important configuration and state information. You can invoke the display summary these three ways:

  • Type the UDP object variable name at the command line.

  • Exclude the semicolon when creating a UDP object.

  • Exclude the semicolon when configuring properties using the dot notation.

You can also display summary information via the Workspace browser by right-clicking an instrument object and selecting Display Summary from the context menu.

The display summary for the UDP object u is given below.

UDP Object : UDP-

Communication Settings 
   RemotePort:         4012
   Terminator:         'LF'

Communication State 
   Status:             closed
   RecordStatus:       off

Read/Write State  
   TransferStatus:     idle
   BytesAvailable:     0
   ValuesReceived:     0
   ValuesSent:         0

Enable Port Sharing over UDP

UDP ports can be shared by other applications to allow for multiple applications to listen to the UDP datagrams on that port. You can bind a UDP object to a specific LocalPort number, and in another application bind a UDP socket to that same local port number so both can receive UDP broadcast data.

This allows for the ability to listen to UDP broadcasts on the same local port number in both MATLAB and other applications. You can enable and disable this capability with a new property of the UDP object called EnablePortSharing.

The EnablePortSharing property allows you to control UDP port sharing, and the possible values are on and off. The default value is off.

EnablePortSharing ValuesResult

Allows other UDP sockets to bind to the UDP object’s LocalPort.

'off' (default)

Prevents other UDP sockets from binding to the UDP object’s LocalPort.

Note that you need to set this property before calling fopen on the UDP object, or you will get an error.

This example shows creating a UDP object, assigning the local port, enabling port sharing, then opening the connection.

u = udp();
u.LocalPort = 5000;
u.EnablePortSharing = 'on';

You can now do read and write operations, and other applications can access the port since port sharing is enabled.