Main Content

coder.mapping.api.get

Get code mappings for model

Since R2020b

    Description

    example

    coderMapObj = coder.mapping.api.get(simulinkModel) returns the active code mappings for the specified model as object coderMapObj. Code mappings associate model data elements with configurations for C or C++ code generation. If a model has multiple code mappings, the active code mappings are the mappings associated with the active system target file.

    If code mappings do not exist, an error is generated. Simulink® creates a code mappings object when you open a coder app for the model. If you have not opened a coder app for the model, you can invoke the coder.mapping.utils.create function to create a code mappings object.

    Examples

    collapse all

    Use the programmatic interface to create and use code mappings objects of Simulink models.

    Load the model NoCoderMapping.

    simulinkModel = "NoCoderMapping";
    load_system(simulinkModel);

    Use a try-catch block to determine whether a code mappings object exists for the model. Inside the try block, attempt to retrieve the existing object by using function coder.mapping.api.get. Inside the catch block, create a new code mappings object by using the function coder.mapping.utils.create. Store the code mappings object in the variable codeMapObj.

    Add print messages to show whether the model had an existing code mappings object or not.

    try
      codeMapObj = coder.mapping.api.get(simulinkModel);
              fprintf("" + ...
                " ========================================================\n" + ...
                " The model already had code mappings.\n" + ...
                " ========================================================\n");
    catch
              fprintf("" + ...
                " ==========================================\n" + ...
                " The model does not have code mappings.\n" + ...
                " Creating new code mappings for the model.\n" + ...
                " ==========================================\n");
      codeMapObj = coder.mapping.utils.create(simulinkModel);
    end
     ==========================================
     The model does not have code mappings.
     Creating new code mappings for the model.
     ==========================================
    

    Retrieve the storage class of input port inport_1 of the model.

    getInport(codeMapObj,"inport_1","StorageClass")
    ans = 
    'Auto'
    

    Set the storage class of input port inport_1 to ExportedGlobal.

    setInport(codeMapObj,"inport_1",StorageClass="ExportedGlobal")

    Use the same try-catch block to see how the function coder.mapping.api.get is now able to retrieve the existing code mappings of the model.

    try
      codeMapObj = coder.mapping.api.get(simulinkModel);
              fprintf("" + ...
                " ========================================================\n" + ...
                " The model already had code mappings.\n" + ...
                " ========================================================\n");
    catch
              fprintf("" + ...
                " ==========================================\n" + ...
                " The model does not have code mappings.\n" + ... 
                " Creating new code mappings for the model.\n" + ...
                " ==========================================\n");
      codeMapObj = coder.mapping.utils.create(simulinkModel);
    end
     ========================================================
     The model already had code mappings.
     ========================================================
    

    Retrieve the storage class of input port inport_1 of the model. Notice that it is the storage class that you set previously.

    getInport(codeMapObj,"inport_1","StorageClass")
    ans = 
    'ExportedGlobal'
    

    Close the model without saving it.

    close_system(simulinkModel,false)

    Input Arguments

    collapse all

    Simulink model for which to return the code mapping object, specified as a handle, or a character vector or string scalar with the model name (without the .slx file extension), or the relative or absolute path of the model file (including the .slx file extension).

    Note

    The model must be loaded (for example, by using load_system).

    Example: "configModel"

    Data Types: char | string | handle

    Data interface dictionary for which to return the code mappings object, specified as a Simulink.data.Dictionary object, or a character vector or string scalar with the relative or full path of the dictionary file (including the .sldd file extension).

    Example: "exCodeDefs.sldd"

    Data Types: char | string | Simulink.data.Dictionary

    Output Arguments

    collapse all

    The code mappings object for the model, returned as a CodeMapping object or a CodeMappingCPP.

    OutputInput ObjectCode Mapping Type
    coder.mapping.api.CodeMappingSimulink model"SimulinkCoderC" or "EmbeddedCoderC"
    coder.mapping.api.CodeMappingCPP (Embedded Coder)Simulink model"EmbeddedCoderCPP"

    The Embedded Coder data dictionary, returned as a coder.mapping.api.CoderDictionary object.

    Version History

    Introduced in R2020b