主要内容

使用使用的MEX功能MATLAB编码器应用程序

Workflow for Generating MEX Functions Using theMATLAB编码器应用程序

Step Action Details
1

Set up theMATLAB®编码器™project.

Set Up a MATLAB Coder Project
2

Specify the build configuration parameters. SetBuild typeto梅克斯

Configure Project Settings
3

Build the project.

建造一个MATLAB Coder Project

TheMATLAB Coder应用程序不受支持金宝appMATLAB Online™。To generate MEX functions inMATLAB Online, use thecodegencommand.

Generate a MEX Function Using theMATLAB编码器应用程序

This example shows how to generate a MEX function from MATLAB code using theMATLAB Coder应用程序。

创建入口点函数

In a local writable folder, create a MATLAB file,麦克达。m,包含:

功能Y = Mcadd(U,V)%#codegeny = u + v;

Create the Test File

在相同的本地可写文件夹中,创建一个matlab文件,Mcadd_test.m.,那个呼叫麦克达使用示例输入。示例输入是具有类型的标量INT16

功能y = mcadd_test y = mcadd(int16(2), int16(3));

Open theMATLAB编码器应用程序

On the MATLAB Toolstrip应用程序stab, underCode Generation, 点击MATLAB Coder应用图标。

The app opens theSelect Source Filespage.

指定源文件

  1. On theSelect Source Filespage, type or select the name of the entry-point function麦克达

    The app creates a project with the default nameMcadd.prj.

  2. Click下一个定义输入类型步。该应用程序分析了编码问题和代码生成准备的功能。如果应用程序识别问题,它将打开审核代码生成准备page where you can review and fix issues. In this example, because the app does not detect issues, it opens the定义输入类型page.

定义输入类型

因为C在编译时使用静态键入,MATLAB Coder必须确定MATLAB文件中所有变量的属性。您必须指定所有入学点函数输入的属性。从入口点函数输入的属性,MATLAB Coder可以推断Matlab文件中所有变量的属性。

Specify the test fileMcadd_test.m.MATLAB Coderuses to automatically define types foruv:

  1. Enter or select the test fileMcadd_test.m.

  2. Click自动杂记输入类型

    The test file,Mcadd_test.m.调用的入口点函数,麦克达, with the example input types.MATLAB Coderinfers输入uvareINT16(1x1)

  3. Click下一个Check for Run-Time Issues步。

Check for Run-Time Issues

TheCheck for Run-Time Issuesstep generates a MEX file from your entry-point functions, runs the MEX function, and reports issues. This step is optional. However, it is a best practice to perform this step. You can detect and fix run-time errors that are harder to diagnose in the generated C code.

  1. To open theCheck for Run-Time Issues对话框,单击Check for Issues

    The app populates the test file field with麦克达_test, the test file that you used to define the input types.

  2. ClickCheck for Issues

    The app generates a MEX function. It runs the test file replacing calls to麦克达呼叫mex函数。如果应用程序在MEX函数生成或执行期间检测到问题,则提供警告和错误消息。单击这些消息以导航到有问题的代码并修复问题。在此示例中,该应用程序不会检测到问题。

  3. Click下一个生成代码步。

Generate the MEX Function

  1. To open theGenerate对话框,单击Generate

  2. 在里面Generate对话框,设置Build typeto梅克斯Languageto C. Use the default values for the other project build configuration settings.

  3. ClickGenerate

    The app indicates that code generation succeeded. It displays the source MATLAB files and the generated output files on the left side of the page. On the变量tab, it displays information about the MATLAB source variables. On the目标构建日志tab, it displays the build log, including compiler warnings and errors.

    MATLAB Coderbuilds the project and, by default, generates a MEX function,麦克达_mex,在当前文件夹中。MATLAB Coder还在调用的子文件夹中生成其他支持文件金宝appCodegen / MEX / MCADDMATLAB Coder使用MATLAB函数的名称作为生成文件的根名称。它为MEX文件创建了特定于平台的扩展。看Naming Conventions

  4. To view the code generation report, clickView Report

  5. Click下一个to open the完成工作流程page.

查看完成工作流程页面

The完成工作流程page indicates that code generation succeeded. It provides a project summary and links to the generated output.

Configure Project Settings

要打开“项目设置”对话框:

  1. To open theGenerate对话框,单击Generate

  2. ClickMore Settings

To change a project setting, click the tab that contains the setting that you want to change. For example, to change the在整数溢出上饱和setting, click theSpeedtab.

MEX功能使用不同的配置参数集而不是库和可执行文件。当您从中更改输出类型时梅克斯Functionto源CodeStatic Library,Dynamic Library, orExecutable,验证这些设置。

某些配置参数与MEX和独立代码生成相关。如果在输出类型是时启用任何这些参数梅克斯Function, 和you want to use the same setting for C/C++ code generation as well, you must enable it again forC/C++ Static Library,C/C++ Dynamic Library, 和C/C++ Executable

看Also

建造一个MATLAB编码器Project

To build a project using the specified settings, on the生成代码page, clickGenerate。作为MATLAB Coderapp builds a project, it displays the build progress. When the build is complete, the app provides details about the build on the目标构建日志tab.

如果启用代码生成报告或发生构建错误,则该应用程序会生成报告。该报告提供有关最新构建的详细信息,并提供报告的链接。

To view the report, click theView report关联。该报告提供了与MATLAB代码的链接,并生成C / C ++文件以及MATLAB代码中变量的编译时类型信息。如果发生构建错误,则报告列出了错误和警告。

看Also

相关话题