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.

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

    example

    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 = 
    302.0016
    

    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, specified as one of these values:

    • "Auto"

    • "ExportedGlobal"

    • "ImportedExtern"

    • "ImportedExternPointer"

    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

    Version History

    Introduced in R2020b