0

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.

GreySage
  • 1,153
  • 19
  • 39

2 Answers2

4

This answer assumes that the DSP System Toolbox is not available. If it is, the System Objects dsp.UDPSender and dsp.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

  1. Write C(++) functions implementing the behaviour you need or find a C library providing the necessary functionality. Assume we implement the function externalUDPSend in the files externalUDPSend.h/.c.
  2. 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 like callfoo in the linked example will work:

    function y = useExternalUDP(x)
    %#codegen
    if coder.target('MATLAB')
      % Running in MATLAB. Use standard MATLAB
      % network I/O code here
      ...
    else
      % Generating code. Call external code/library
      % Include header for external code
      coder.cinclude('externalUDPSend.h');
    
      % Set the type of the output. Assume double scalar
      % Change the RHS to match the return type
      y = 0;
      y = coder.ceval('externalUDPSend',x,numel(x));
    end
    
  3. Develop your project. Call the wrapper function(s), which will work in MATLAB and in the generated code because of the use of coder.target.
  4. Generate a MEX function using something like:

    codegen useExternalUDP -config:mex externalUDPSend.c -args ...
    

    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.

  5. Generate standalone code and either let MATLAB Coder compile it to a library or deploy the code to an external IDE and compile it there.

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 or coder.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 functions fprintf and fread are supported for code generation so this example is meant to be instructive rather than a necessity for doing file I/O.

Ryan Livingston
  • 1,898
  • 12
  • 18
Daniel
  • 36,610
  • 3
  • 36
  • 69
  • 1
    I don't think it is necessary to hand write a C MEX function in step 1. Rather, it can be plain C code, without the MEX interface, that does the network necessary network communication. Then use `coder.ceval` to call that C code from your MATLAB code as shown in the example. To test in MATLAB, use MATLAB Coder to generate a MEX file that calls your custom code using `codegen ... -config:mex...` or `c = coder.config('mex');`. Once testing finishes, generate code as needed. – Ryan Livingston Jul 27 '15 at 20:57
  • I think this will solve my problem, even though it's not strictly an answer to the question. To be clear, you advocate running the network code in the mex file? – GreySage Jul 27 '15 at 21:53
  • My comment was meant to clarify the requirements for step 1. Testing the generated MEX file is highly recommend because it can report runtime errors whereas the generated code won't have those error checks. Also it lets you test in the interactive MATLAB environment. Once the MEX runs well, you can generate standalone code with confidence. – Ryan Livingston Jul 27 '15 at 22:06
  • @lilbill39: You definitely know the matlab coder better than I do. Made the answer a wiki answer, if you think something must be changed please update it. – Daniel Jul 27 '15 at 22:19
  • @Daniel, thanks, it has been updated. I actually work on MATLAB Coder, so that's how I've picked up my knowledge of it. – Ryan Livingston Jul 28 '15 at 03:11
1

If you have the DSP System Toolbox, the System Objects dsp.UDPSender and dsp.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.

Ryan Livingston
  • 1,898
  • 12
  • 18