Using Legacy code tool
- The Legacy toolbox can generate, compile and link, and create a masked block for the specified S-function.
- Legacy Code Tool helps users create S-functions automatically from supplied specifications expressed with MATLAB code
legacy_code('sfcn_cmex_generate', specs)
generates an S-function source file as specified by the Legacy Code Tool data structure, specs.
legacy_code('compile', specs, compilerOptions)
compiles and links the S-function generated by the Legacy Code Tool based on the data structure, specs, and compiler options that you might specify. The compiler options must be supported by the mex (MATLAB) function.
legacy_code('generate_for_sim', specs, modelname)
generates, compiles, and links the S-function in a single step. If the Options.useTlcWithAccel field of the Legacy Code Tool data structure is set to logical 1 (true), the function also generates a TLC file for accelerated simulations.
legacy_code('slblock_generate', specs, modelname)
generates a masked S-Function block for the S-function generated by the Legacy Code Tool based on the data structure, specs. The block appears in the Simulink® model specified by modelname. If you omit modelname, the block appears in an empty model editor window.
The InitializeConditionsFcnSpec, OutputFcnSpec, StartFcnSpec, and TerminateFcnSpec fields defined in the Legacy Code Tool data structure require character vector values that adhere to a specific syntax format. The required syntax format enables the Legacy Code Tool to map the return value and arguments of an existing C function to the return value, inputs, outputs, parameters, and work vectors of the S-function that the tool generates.
General syntax
return-spec = function-name(argument-spec)
For example, the following character vector specifies a function named doubleIt with return specification double y1 and input argument specification double u1.
def.OutputFcnSpec = 'double y1 = doubleIt(double u1)';
Using command:
specs.HostLibFiles = {['exlib',strrep(system_dependent('GetSharedLibExt'),'.dll','.lib')]};
mentioned here
- There should be two implementations of the function:[1]
- One for simulating your model on the host computer
- other for the target hardware
- The Legacy Code Tool can interface with C++ functions, but not C++ objects