Customize C Step and Initialize Function Interfaces Programmatically

To configure initialize and step entry-point function interfaces for a rate-based model programmatically, use these functions.

FunctionDescription
addArgConfAdd step function argument configuration information for Simulink® model port to model-specific C function prototype
attachToModelAttach model-specific C function prototype to loaded ERT-based Simulink model
getArgCategoryGet step function argument category for Simulink model port from model-specific C function prototype
getArgNameGet step function argument name for Simulink model port from model-specific C function prototype
getArgPositionGet step function argument position for Simulink model port from model-specific C function prototype
getArgQualifierGet step function argument type qualifier for Simulink model port from model-specific C function prototype
getDefaultConfGet default configuration information for model-specific C function prototype from Simulink model to which it is attached
getFunctionNameGet function name from model-specific C function prototype
getNumArgsGet number of step function arguments from model-specific C function prototype
getPreviewGet model-specific C function prototype code previews
RTW.configSubsystemBuildOpen UI to configure C function prototype or C++ class interface for right-click build of specified subsystem
RTW.getFunctionSpecificationGet handle to model-specific C function prototype object
setArgCategorySet step function argument category for Simulink model port in model-specific C function prototype
setArgNameSet step function argument name for Simulink model port in model-specific C function prototype
setArgPositionSet step function argument position for Simulink model port in model-specific C function prototype
setArgQualifierSet step function argument type qualifier for Simulink model port in model-specific C function prototype
setFunctionNameSet function name in model-specific C function prototype

Typical uses of these functions include:

  • Create a function interface.

  • Modify an existing function interface.

  • Create a function interface, starting with default configuration information from a model.

  • Reset the model function interface to the default ERT function configuration.

Create Function Interface

  1. Create a model-specific C function interface with obj = RTW.ModelSpecificCPrototype, where obj returns a handle to a new, empty function interface.

  2. Add argument configuration information for your model ports by using addArgConf.

  3. Attach the function interface to your loaded ERT-based model by using attachToModel.

  4. Save your model.

  5. Generate code by using the rtwbuild function.

Modify an Existing Function Interface

  1. Get the handle to an existing model-specific C function interface that is attached to your loaded ERT-based model with obj = RTW.getFunctionSpecification(modelName). modelName is a character vector specifying the name of a loaded ERT-based model. obj returns a handle to a function interface attached to the specified model.

    You can use other functions on the returned handle only if the test isa(obj,'RTW.ModelSpecificCPrototype') returns 1. If the model does not have a function interface configuration, the function returns []. If the function returns a handle to an object of type RTW.FcnDefault, you cannot modify the existing function interface.

  2. Use the Get and Set functions to test and reset such items as the function names, argument names, argument positions, argument categories, and argument type qualifiers.

  3. Save your model.

  4. Generate code by using the rtwbuild function.

Create Function Interface Starting With Default Configuration From Model

  1. Create a model-specific C function interface by using obj = RTW.ModelSpecificCPrototype, where obj returns a handle to a new, empty function interface.

  2. Attach the function interface to your loaded ERT-based model by using attachToModel.

  3. Get default configuration information from your model by using getDefaultConf.

  4. Use the Get and Set functions to test and reset such items as the function names, argument names, argument positions, argument categories, and argument type qualifiers.

  5. Save your model.

  6. Generate code by using the rtwbuild function.

Reset Model Function Interface to Default ERT Function Configuration

Create an object of the ERT default function interface. Reset the model function interface and undo custom settings by calling the RTW.FcnDefault method, attachToModel:

obj = RTW.FcnDefault;
obj.attachToModel(model);
model must be a loaded ERT-based model.

Do not use the same model-specific C function interface object across multiple models. If you do, changes that you make to the step and initialize functions in one model are propagated to other models, which is usually not what you want.

Sample Script for Configuring Function Interfaces

This example MATLAB® script configures the model function interfaces for example model rtwdemo_counter.

%% Open the rtwdemo_counter model
rtwdemo_counter

%% Select ert.tlc as the System Target File for the model
set_param(gcs,'SystemTargetFile','ert.tlc')

%% Create a model-specific C function prototype
a=RTW.ModelSpecificCPrototype

%% Add argument configuration information for Input and Output ports
addArgConf(a,'Input','Pointer','inputArg','const *')
addArgConf(a,'Output','Pointer','outputArg','none')

%% Attach the model-specific C function prototype to the model
attachToModel(a,gcs)

%% Rename the initialization function
setFunctionName(a,'InitFunction','init')

%% Rename the step function and change some argument attributes
setFunctionName(a,'StepFunction','step')
setArgPosition(a,'Output',1)
setArgCategory(a,'Input','Value')
setArgName(a,'Input','InputArg')
setArgQualifier(a,'Input','none')

%% Generate code and build
rtwbuild(gcs)

Related Topics