Main Content

Exception to Using the Library Functions that Access model.rtw

There are several library functions that provide access to block inputs, outputs, parameters, sample times, and other information. It is recommended that you use these library functions to access many of the parameter name/parameter values pairs in the block record, as opposed to accessing the parameter name/parameter value pairs directly from your block TLC code. For more information about using these functions (recommended method for accessing model.rtw), see Target Language Compiler Library Functions Overview.

An exception to using these functions is when you access parameter settings for a block. Parameter settings can be written out using the mdlRTW function of a C MEX S-function. They can contain data in the form of strings, scalar values, vectors, and matrices. They can be used to pass fixed values and information that is used to alter the generated code for a block or directly as values in the resulting code of a block.

Example Exception to Using the Library Functions

The following example demonstrates accessing parameter settings for a block using the mdlRTW function of a C MEX S-function. For more details on using parameter settings, see Inlining S-Functions.

mdlRTW Function in C MEX S-Function Code

static void mdlRTW(SimStruct *S)
{
    if (!ssWriteRTWParamSettings( S, 1, SSWRITE_VALUE_QSTR, "Operator", "AND"))
    {
        ssSetErrorStatus(S,"Error writing parameter data to .rtw file");
        return;
    }
}

Resulting Block Record in model.rtw File

Block {
      Type    "S-Function"
      Name    "<Root>/S-Function"

      ...

      SFcnParamSettings {
        Operator    "AND"
      }
    }

TLC Code to Access the Parameter Settings

%function Outputs(block, system) Output
  %%
  %% Select Operator
  %switch(SFcnParamSettings.Operator)
    %case "AND"
      %assign LogicOp        = "&"
      %break
    ...
  %endswitch
%endfunction

Caution Against Directly Accessing Record Fields

When functions in the block target file are called, they are passed to the block and system records for this instance as arguments. The first argument, block, is in scope, which means that variable names inside this instance’s block record are accessible by name. For example:

%assign fast = SFcnParamSetting.Fast

Block target files could generate code for a given block by directly using the fields in the Block record for the block. This process is not recommended, for two reasons:

  • The contents of the model.rtw file can change from release to release. This can cause block TLC files that access the model.rtw file directly to stop working.

  • TLC library functions are provided that substantially reduce the amount of TLC code for implementing a block while handling the various configurations (widths, data types, etc.) a block might have. These library functions are provided by the system target files to provide access to inputs, outputs, parameters, and so on. Using these functions in a block TLC script makes it flexible enough to generate code for multiple instances or configurations of the block, as well as across releases. Exceptions to this do occur, however, such as when you want to directly access a field in the block’s record. This happens with parameter settings, as discussed in TLC Code to Access the Parameter Settings.

Related Topics