主要内容

coder.mapping.api.get

获取模型的代码映射

    说明

    代码映射coderMapObj = coder.mapping.api.get(simulinkModel) 将指定模型的活动代码映射以对象 coderMapObj 形式返回。代码映射将模型数据元素与用于 C 或 C++ 代码生成的配置相关联。如果一个模型具有多个代码映射,则活动代码映射是与活动系统目标文件关联的映射。

    如果代码映射不存在,则会生成错误。Simulink® 会在您为模型打开代码生成器时创建一个代码映射对象。如果您尚未为该模型打开代码生成器,则可以调用 coder.mapping.utils.create 函数来创建一个代码映射对象。

    示例

    示例

    全部折叠

    使用编程接口创建和使用 Simulink 模型的代码映射对象。

    加载模型 NoCoderMapping

    simulinkModel = "NoCoderMapping";
    load_system(simulinkModel);

    使用 try-catch 代码块确定该模型是否存在代码映射对象。在 try 模块中,尝试使用函数 coder.mapping.api.get 检索现有对象。在 catch 代码块中,使用函数 coder.mapping.utils.create 创建一个新代码映射对象。将代码映射对象存储在变量 codeMapObj 中。

    添加打印消息以显示模型是否具有现有代码映射对象。

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

    检索模型的输入端口 inport_1 的存储类。

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

    将输入端口 inport_1 的存储类设置为 ExportedGlobal

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

    使用同一个 try-catch 代码块查看函数 coder.mapping.api.get 现在如何能够检索模型的现有代码映射。

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

    检索模型的输入端口 inport_1 的存储类。请注意,这是您之前设置的存储类。

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

    关闭模型而不保存它。

    close_system(simulinkModel,false)

    输入参数

    全部折叠

    要为其返回代码映射对象的 Simulink 模型,指定为句柄,或包含模型名称(不带 .slx 文件扩展名)的字符向量或字符串标量,或模型文件的相对或绝对路径(包括 .slx 文件扩展名)。

    注意

    模型必须已加载(例如,通过使用 load_system)。

    示例: "configModel"

    数据类型: char | string | handle

    要为其返回代码映射对象的数据接口字典,指定为 Simulink.data.Dictionary 对象,或包含字典文件的相对或完整路径(包括 .sldd 文件扩展名)的字符向量或字符串标量。

    示例: "exCodeDefs.sldd"

    数据类型: char | string | Simulink.data.Dictionary

    输出参量

    全部折叠

    模型的代码映射对象,以 CodeMapping 对象或 CodeMappingCPP 形式返回。

    输出输入对象代码映射类型
    coder.mapping.api.CodeMappingSimulink 模型"SimulinkCoderC""EmbeddedCoderC"
    coder.mapping.api.CodeMappingCPP (Embedded Coder)Simulink 模型"EmbeddedCoderCPP"

    Embedded Coder 数据字典,以 coder.mapping.api.CoderDictionary 对象形式返回。

    版本历史记录

    在 R2020b 中推出