Main Content


Set default storage class and storage class property values for model data category

Since R2020b



    setDataDefault(coderMapObj,elemCategory,Name=Value) sets the default storage class and storage class property values in the code mappings for the specified category of model data.

    You cannot specify default data interfaces for models with an attached Embedded Coder Dictionary that defines a service interface configuration.


    collapse all

    Use the programmatic interface to get and set the data defaults in the code mappings configuration of a Simulink model.

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

    Open the model CoderMapAPI.

    simulinkModel = "CoderMapAPI";

    Retrieve the code mappings object of the model.

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

    Set the storage class of all root-level ports to Model default so that their generated code is determined by the value you set for Data Defaults.

    setInport(codeMapObj,find(codeMapObj,"Inport"),StorageClass="Model default")
    setOutport(codeMapObj,find(codeMapObj,"Outport"),StorageClass="Model default")

    Determine the default storage classes for Input and output ports.

    inPortDefaultStorageClass = getDataDefault(codeMapObj,"Inports","StorageClass")
    inPortDefaultStorageClass = 
    outPortDefaultStorageClass = getDataDefault(codeMapObj,"Outports","StorageClass")
    outPortDefaultStorageClass = 

    Generate code from the model.


    Root-level ports with ImportedExtern and ImportedExternPointer storage classes are declared in the generated private header file of the model in separate sections.

    Store the name of the header file.

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

    These are the declarations of the root-level ports in the header file:

    /* Data with Imported storage */
    extern real_T in_port_1;               /* '<Root>/in_port_1' */
    extern real_T in_port_2;               /* '<Root>/in_port_2' */
    extern real_T in_port_3;               /* '<Root>/in_port_3' */
    extern real_T in_port_4;               /* '<Root>/in_port_4' */
    /* Data with Imported storage (pointer) */
    extern real_T *out_port_1;             /* '<Root>/out_port_1' */
    extern real_T *out_port_2;             /* '<Root>/out_port_2' */
    extern real_T *out_port_3;             /* '<Root>/out_port_3' */
    extern real_T *out_port_4;             /* '<Root>/out_port_4' */

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


    Switch between the default storage class of input and output ports.


    Generate the code from the revised model.


    The declarations of the root-level ports are updated in the header file according to the specified storage classes.

    /* Data with Imported storage */
    extern real_T out_port_1;              /* '<Root>/out_port_1' */
    extern real_T out_port_2;              /* '<Root>/out_port_2' */
    extern real_T out_port_3;              /* '<Root>/out_port_3' */
    extern real_T out_port_4;              /* '<Root>/out_port_4' */
    /* Data with Imported storage (pointer) */
    extern real_T *in_port_1;              /* '<Root>/in_port_1' */
    extern real_T *in_port_2;              /* '<Root>/in_port_2' */
    extern real_T *in_port_3;              /* '<Root>/in_port_3' */
    extern real_T *in_port_4;              /* '<Root>/in_port_4' */

    Input Arguments

    collapse all

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

    Example: myCM

    Category of model data element for which to set the storage class and storage class properties.

    Example: "Inports"

    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"

    Storage class to set for the specified data element category. The name of a predefined storage class or storage class that is defined in the Embedded Coder Dictionary associated with the model. Values that you can specify vary depending on the category that you specify. See Choose Storage Class for Controlling Data Representation in Generated Code.

    Example: StorageClass="ImportedExtern"

    Version History

    Introduced in R2020b