You create a UDP object with the
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
if you will use the object to read data from the instrument.
For example, to create a UDP object associated with the remote host 127.0.0.1, remote port 4012, and local port 3533,
u = udp('127.0.0.1', 4012, 'LocalPort', 3533);
The UDP object
u now exists in the MATLAB® workspace.
You can display the class of
u with the
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
These general purpose properties provide information about the UDP
UDP Descriptive Properties
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
u with the
u.Name ans = UDP-127.0.0.1
u.RemoteHost ans = 127.0.0.1
u.RemotePort ans = 4012
u.Type ans = udp
u.LocalPort' ans = 3533
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
UDP Object : UDP-127.0.0.1 Communication Settings RemotePort: 4012 RemoteHost: 127.0.0.1 Terminator: 'LF' Communication State Status: closed RecordStatus: off Read/Write State TransferStatus: idle BytesAvailable: 0 ValuesReceived: 0 ValuesSent: 0
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
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 property allows you
to control UDP port sharing, and the possible values are
The default value is
Allows other UDP sockets to bind to the UDP object’s
Prevents other UDP sockets from binding to the UDP object’s
Note that you need to set this property before calling
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'; fopen(u)
You can now do read and write operations, and other applications can access the port since port sharing is enabled.