I'm trying to get very basic network functionality with Matlab Coder (I need to turn it into C code). However, all the network classes and objects I try arn't supported by Coder. It seems unreasonable that Matlab would completely neglect networking entirely with this tool. Is there some method of sending data over a network that DOES work with coder?
I'd prefer TCP, but UDP or anything else that will actually send/receive data will work, as long as it is compatible with Coder.
This answer assumes that the DSP System Toolbox is not available. If it is, the System Objects
dsp.UDPSender
anddsp.UDPReceiver
may be considered.Since the final goal is to generate C code, and because network I/O is usually done via a library, a good approach would be to integrate external C code that does the network I/O into your MATLAB Code. The basic way to call an external C function is using
coder.ceval
and the process is explained here.Recommended Steps
externalUDPSend
in the filesexternalUDPSend.h/.c
.Write one or more MATLAB functions that call your C(++) functions using
coder.ceval
as shown in the linked documentation. These will serve as a wrapper around your external code, and will expose the C(++) code to MATLAB. Something likecallfoo
in the linked example will work:coder.target
.Generate a MEX function using something like:
The generated MEX function serves as the MATLAB interface to your custom code so there is no need to hand write a MEX interface. MATLAB Coder will generate all of MEX interfacing logic for you. Then test that MEX function in MATLAB. Testing the MEX function is important because runtime errors like out of bounds indexing, using features not supported for code generation, etc. can be detected and reported in MEX. These checks are removed from generated standalone code.
Integrating External Libraries/Encapsulating Dependencies
Note that you may also need to link in libraries if you choose to use an existing network I/O library, or you may need to modify the build of the generated code. You can either use
coder.updateBuildInfo
orcoder.ExternalDependency
to achieve this in your MATLAB Code.Further Reading
The file reading example shows some more advanced custom code integration tools such as
coder.ref
,coder.opaque
, and dealing with C strings from MATLAB code when calling external code. Note that the MATLAB functionsfprintf
andfread
are supported for code generation so this example is meant to be instructive rather than a necessity for doing file I/O.If you have the DSP System Toolbox, the System Objects
dsp.UDPSender
anddsp.UDPReceiver
are supported for code generation since they are listed in the comprehensive list of supported functions.The code generated from them relies on prebuilt libraries shipped with MATLAB and will run on desktop platforms compatible with those libraries. See the documentation for the UDP Receive block for more details.