MATLAB Examples

Integrate External C Functions That Pass Arguments That Have Inherited Dimensions

This example shows how to use the Legacy Code Tool to integrate legacy C functions whose arguments have inherited dimensions.

With the Legacy Code Tool, you can:

  • Provide the legacy function specification.
  • Generate a C-MEX S-function that calls the legacy code during simulation.
  • Compile and build the generated S-function for simulation.
  • Generate a TLC block file and optional rtwmakecfg.m file that specifies how the generated code for a model calls the legacy code.

Contents

Provide the Legacy Function Specification

Legacy Code Tool functions take a specific data structure or array of structures as the argument. You can initialize the data structure by calling the function legacy_code() using 'initialize' as the first input. After initializing the structure, assign its properties to values corresponding to the legacy code being integrated. For detailed help on the properties, call legacy_code('help'). The prototypes of the legacy functions being called in this example are:

  • void mat_add(real_T *u1, real_T *u2, int32_T nbRows, int32_T nbCols, real_T *y1)
  • void mat_mult(real_T *u1, real_T *u2, int32_T nbRows1, int32_T nbCols1, int32_T nbCols2, real_T *y1)

real_T is a typedef to double, and int32_T is a typedef to a 32-bit integer. The legacy source code is in the files mat_ops.h, and mat_ops.c.

defs = [];

% rtwdemo_sfun_mat_add
def = legacy_code('initialize');
def.SFunctionName = 'rtwdemo_sfun_mat_add';
def.OutputFcnSpec = ['void mat_add(double u1[][], double u2[][], ' ...
                   'int32 u3, int32 u4, double y1[size(u1,1)][size(u1,2)])'];
def.HeaderFiles   = {'mat_ops.h'};
def.SourceFiles   = {'mat_ops.c'};
def.IncPaths      = {'rtwdemo_lct_src'};
def.SrcPaths      = {'rtwdemo_lct_src'};
defs = [defs; def];

% rtwdemo_sfun_mat_mult
def = legacy_code('initialize');
def.SFunctionName = 'rtwdemo_sfun_mat_mult';
def.OutputFcnSpec =  ['void mat_mult(double u1[p1][p2], double u2[p2][p3], '...
                   'int32 p1, int32 p2, int32 p3, double y1[p1][p3])'];
def.HeaderFiles   = {'mat_ops.h'};
def.SourceFiles   = {'mat_ops.c'};
def.IncPaths      = {'rtwdemo_lct_src'};
def.SrcPaths      = {'rtwdemo_lct_src'};
defs = [defs; def];

Generate S-Functions for Simulation

To generate C-MEX S-functions according to the description provided by the input argument 'defs', call the function legacy_code() again with the first input set to 'sfcn_cmex_generate'. The S-functions call the legacy functions during simulation. The source code for the S-function is in the files rtwdemo_sfun_mat_add.c and rtwdemo_sfun_mat_mult.c.

legacy_code('sfcn_cmex_generate', defs);

Compile the Generated S-Functions for Simulation

After you generate the C-MEX S-function source files, to compile the S-functions for simulation with Simulink®, call the function legacy_code() again with the first input set to 'compile'.

legacy_code('compile', defs);
### Start Compiling rtwdemo_sfun_mat_add
    mex('-I/mathworks/devel/bat/Bdoc17b/build/matlab/toolbox/rtw/rtwdemos/rtwdemo_lct_src', '-I/private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex89794148', '-c', '-outdir', '/private/tmp/Bdoc17b_685977_71560/publish_examples4/tp1c182d43_138a_4e0a_9dca_ba56d7502abd', '/mathworks/devel/bat/Bdoc17b/build/matlab/toolbox/rtw/rtwdemos/rtwdemo_lct_src/mat_ops.c')
Building with 'Xcode with Clang'.
MEX completed successfully.
    mex('rtwdemo_sfun_mat_add.c', '-I/mathworks/devel/bat/Bdoc17b/build/matlab/toolbox/rtw/rtwdemos/rtwdemo_lct_src', '-I/private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex89794148', '/private/tmp/Bdoc17b_685977_71560/publish_examples4/tp1c182d43_138a_4e0a_9dca_ba56d7502abd/mat_ops.o')
Building with 'Xcode with Clang'.
MEX completed successfully.
### Finish Compiling rtwdemo_sfun_mat_add
### Exit

### Start Compiling rtwdemo_sfun_mat_mult
    mex('-I/mathworks/devel/bat/Bdoc17b/build/matlab/toolbox/rtw/rtwdemos/rtwdemo_lct_src', '-I/private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex89794148', '-c', '-outdir', '/private/tmp/Bdoc17b_685977_71560/publish_examples4/tpa88e9cd1_e258_4d90_8ce3_db92fd43cb97', '/mathworks/devel/bat/Bdoc17b/build/matlab/toolbox/rtw/rtwdemos/rtwdemo_lct_src/mat_ops.c')
Building with 'Xcode with Clang'.
MEX completed successfully.
    mex('rtwdemo_sfun_mat_mult.c', '-I/mathworks/devel/bat/Bdoc17b/build/matlab/toolbox/rtw/rtwdemos/rtwdemo_lct_src', '-I/private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex89794148', '/private/tmp/Bdoc17b_685977_71560/publish_examples4/tpa88e9cd1_e258_4d90_8ce3_db92fd43cb97/mat_ops.o')
Building with 'Xcode with Clang'.
MEX completed successfully.
### Finish Compiling rtwdemo_sfun_mat_mult
### Exit

Generate TLC Block Files for Code Generation

After you compile the S-functions and use them in simulation, you can call the function legacy_code() again with the first input set to 'sfcn_tlc_generate' to generate TLC block files. Block files specify how the generated code for a model calls the legacy code. If you do not generate TLC block files and you try to generate code for a model that includes the S-functions, code generation fails. The TLC block files for the S-functions are rtwdemo_sfun_mat_add.tlc and rtwdemo_sfun_mat_mult.tlc.

legacy_code('sfcn_tlc_generate', defs);

Generate an rtwmakecfg.m File for Code Generation

After you create the TLC block files, you can call the function legacy_code() again with the first input set to 'rtwmakecfg_generate' to generate an rtwmakecfg.m file to support code generation. If the required source and header files for the S-functions are not in the same folder as the S-functions, and you want to add these dependencies in the makefile produced during code generation, generate the rtwmakecfg.m file.

legacy_code('rtwmakecfg_generate', defs);

Generate Masked S-Function Blocks for Calling the Generated S-Functions

After you compile the C-MEX S-function source, you can call the function legacy_code() again with the first input set to 'slblock_generate' to generate masked S-function blocks that call the S-functions. The software places the blocks in a new model. From there you can copy them to an existing model

legacy_code('slblock_generate', defs);

Show the Generated Integration with Legacy Code

The model rtwdemo_lct_inherit_dims shows integration of the model with the legacy code. The subsystem TestMatOps serves as a harness for the calls to the legacy C functions, with unit delays serving to store the previous output values.

open_system('rtwdemo_lct_inherit_dims')
open_system('rtwdemo_lct_inherit_dims/TestMatOps')
sim('rtwdemo_lct_inherit_dims')