主要内容

coder.mapping.utils.create

创建用于配置 C 和 C++ 代码生成的数据和函数接口的代码映射对象

    说明

    coderMapObj = coder.mapping.utils.create(simulinkModel) 为指定的模型创建一个代码映射环境(如果尚不存在),并将其以对象 coderMapObj 形式返回。代码映射将模型数据元素和函数与用于 C 或 C++ 代码生成的配置相关联。如果指定的模型存在代码映射,则该函数将这些代码映射以对象 coderMapObj 形式返回。

    示例

    示例

    全部折叠

    使用编程接口创建和使用 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

    输出参量

    全部折叠

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

    输出输入对象
    coder.mapping.api.CodeMapping为 C 代码生成配置的 Simulink 模型
    coder.mapping.api.CodeMappingCPP (Embedded Coder)为 C++ 代码生成配置的 Simulink 模型

    版本历史记录

    在 R2020b 中推出