Main Content

addSignal

Add block output signal to model code mappings

Since R2020b

    Description

    addSignal(coderMapObj,portHandle) adds signals specified by the block output port handles to the specified model code mappings.

    This function does not apply to signals that originate from root-level Inport blocks.

    example

    addSignal(coderMapObj,portHandle,Name=Value) specifies one or more name-value arguments in addition to the input argument in the previous syntax.

    Examples

    collapse all

    Use the programmatic interface to add and remove signals in the code mapping configuration of a Simulink model.

    To interactively observe how your commands are reflected in the Code Mappings editor, make sure it is open with the Signals/States tab selected. To learn how to open the Code Mappings editor, see Open the Code Mappings Editor – C.

    Open the model CoderMapAPI.

    simulinkModel = "CoderMapAPI";
    open_system(simulinkModel);

    Use the function coder.mapping.api.get to retrieve the code mapping object of the model.

    codeMapObj = coder.mapping.api.get(simulinkModel);

    Get the handles of the output ports of the four Gain blocks and store them in variables.

    port_handle_1 = get_param(simulinkModel+"/gain_1","PortHandles").Outport;
    port_handle_2 = get_param(simulinkModel+"/gain_2","PortHandles").Outport;
    port_handle_3 = get_param(simulinkModel+"/gain_3","PortHandles").Outport;
    port_handle_4 = get_param(simulinkModel+"/gain_4","PortHandles").Outport
    port_handle_4 = 347.0042
    

    Use the function addSignal to add the signal to the code mappings of the model. Set the storage class of sig_1 and sig_3 to ImportedExtern and the storage class of sig_2 and sig_4 to ImportedExternPointer. Update the model.

    addSignal(codeMapObj,[port_handle_1,port_handle_3],StorageClass="ImportedExtern")
    addSignal(codeMapObj,[port_handle_2,port_handle_4],StorageClass="ImportedExternPointer")
    set_param(simulinkModel,"SimulationCommand","update")

    Use the slbuild command to generate code from the model.

    evalc("slbuild(simulinkModel)");

    Signals with ImportedExtern and ImportedExternPointer storage classes are declared in separate sections of the generated private header file of the model. Store the name of the private header file in the variable priv_h_file.

    priv_h_file = fullfile(simulinkModel+"_grt_rtw",simulinkModel+"_private.h")
    priv_h_file = 
    "CoderMapAPI_grt_rtw/CoderMapAPI_private.h"
    

    The declarations of the signals appear as the following code in the header file:

    /* Imported (extern) block signals */
    extern real_T sig__1;              /* '<Root>/gain_1' */
    extern real_T sig__3;              /* '<Root>/gain_3' */
    extern real_T in_port_1;           /* '<Root>/in_port_1' */
    extern real_T in_port_2;           /* '<Root>/in_port_2' */
    
    /* Imported (extern) pointer block signals */
    extern real_T *sig__2;             /* '<Root>/gain_2' */
    extern real_T *sig__4;             /* '<Root>/gain_4' */
    extern real_T *in_port_3;          /* '<Root>/in_port_3' */
    extern real_T *in_port_4;          /* '<Root>/in_port_4' */
    

    The signals are defined according to the specified storage classes.

    To open the header file, enter this command in the MATLAB® Command Window.

    edit(priv_h_file)
    

    Use the function removeSignal to remove the first two signal lines from the code mappings of the model. Update the model.

    removeSignal(codeMapObj,[port_handle_1,port_handle_2])
    set_param(simulinkModel,"SimulationCommand","update")

    Rebuild the model with the slbuild command.

    evalc("slbuild(simulinkModel)"); 

    The updated sections appear as the following code in the header file.

    /* Imported (extern) block signals */
    extern real_T sig__3;              /* '<Root>/gain_3' */
    extern real_T in_port_1;           /* '<Root>/in_port_1' */
    extern real_T in_port_2;           /* '<Root>/in_port_2' */
    
    /* Imported (extern) pointer block signals */
    extern real_T *sig__4;             /* '<Root>/gain_4' */
    extern real_T *in_port_3;          /* '<Root>/in_port_3' */
    extern real_T *in_port_4;          /* '<Root>/in_port_4' */
    

    The removed signals no longer appear in the header file.

    Input Arguments

    collapse all

    Code mapping object (model code mappings) returned by a call to function coder.mapping.api.get.

    Signal to add to the code mappings, specified as the output port handle of the source block of the signal. To add multiple signals simultaneously, specify an output port handle array.

    Example: addSignal(coderMapObj,[outport_1_Handle,outport_2_Handle])

    Name-Value Arguments

    Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

    Example: StorageClass="ExportedGlobal"

    The name of the storage class to set for the specified signals. The storage class is either defined in the internal Embedded Coder® Dictionary of the model, or in a shared Embedded Coder Dictionary that is attached to the model.

    To learn more about signal data configuration for code generation, see Configure Signal Data for C Code Generation.

    Name for the variable that represents the signal data in the generated code.

    Data Types: char | string

    File name for a C source file that contains definitions for global data read by data elements and external code. Applies to storage classes ExportToFile and Volatile.

    Data Types: char | string

    Name of a get function that a data element calls in the generated code. Applies to storage class GetSet.

    Data Types: char | string

    File name for a C header file that contains declarations for global data read by data elements and external code. Applies to storage classes ExportToFile, GetSet, ImportFromFile, and Volatile.

    Data Types: char | string

    Name of the model that owns global data that is used by other models in the same model hierarchy. The code generated for the owner model includes the global data definition. Applies to storage classes ExportToFile and Volatile.

    Data Types: char | string

    When model configuration parameter Array layout is set to Row-major, a flag that indicates whether to preserve dimensions of a data element that is represented in generated code as a multidimensional array. Applies to storage classes ExportToFile, GetSet, ImportFromFile, Localizable, and Volatile.

    Data Types: logical

    Name of a set function that a data element calls in the generated code. Applies to storage class GetSet.

    Data Types: char | string

    Name that the code generator uses to identify the structure for a data element in the generated code. Applies to storage classes Bitfield and Struct.

    Data Types: char | string

    Storage class property defined in the Embedded Coder Dictionary. Values that you can specify vary depending on the storage class definition.

    String or character vector containing the name of a measurement service interface defined in the Embedded Coder Dictionary. By configuring the measurement service interface for signals, states, and data stores, you can preserve the data in the generated code for measurement. To use the dictionary default, specify "Dictionary default". If data from the state does not need to be preserved in the code, specify "Not measured".

    To configure the measurement service interface, the Embedded Coder Dictionary that defines a service interface configuration must be attached to the model. For more information, see Configure Measurement Service Interfaces for Signals, States, and Data Stores.

    Data Types: char | string

    Version History

    Introduced in R2020b