MATLAB 代码分析
识别并修复 MATLAB® 代码中阻止生成 C/C++ 代码的问题
要从 MATLAB 代码成功生成 C/C++ 代码,需要遵循代码生成规则和限制。例如,您的 MATLAB 代码必须仅使用支持代码生成的 MATLAB 函数和工具箱函数。要在生成 C/C++ 代码之前识别和修复 MATLAB 代码的问题,您可以使用代码分析器和代码生成就绪工具进行检查。要检查您的 MATLAB 代码生成的代码是否符合预期,请生成并执行一个 MEX 函数,该函数可在 MATLAB 环境中执行和测试。
函数
coder.runTest | Run test replacing calls to MATLAB functions with calls to MEX functions |
coder.screener | 确定函数是否适合代码生成 |
getLineColumn | Find locations of beginning and end of MATLAB code involved in code generation |
textReport | Export code generation readiness report to base workspace as a character vector (自 R2022a 起) |
属性
coder.CallSite Properties | Information about a function call site in your MATLAB code (自 R2022a 起) |
coder.CodeFile Properties | Description of file containing text that is involved in code generation |
coder.File Properties | Description of file without text that is involved in code generation |
coder.Message Properties | Description of message produced during code generation readiness analysis or during code generation |
coder.ScreenerInfo Properties | Code generation readiness information (自 R2022a 起) |
主题
准备代码生成
- 确定入口函数并检查 MATLAB 代码
在代码生成之前,确定入口函数并检查 MATLAB 代码是否有错误。 - 代码生成就绪工具
代码生成就绪工具会筛查 MATLAB 代码中是否存在代码生成不支持的功能和函数。
调试
- 使用 MEX 函数检查 MATLAB 代码中的问题
使用 MEX 函数验证生成的代码是否提供与原始 MATLAB 代码相同的功能。 - Debugging Strategies
Choose a strategy for detecting and correcting code that is not suitable for code generation. - 调试生成的 MEX 代码
调试从您的 MATLAB 代码生成的 MEX 代码。 - Debug Generated C/C++ Code
Debug standalone C/C++ code generated from your MATLAB code. - 使用 MATLAB 探查器探查 MEX 函数
请在 MATLAB 探查器中查看生成的 MEX 函数的执行时间和代码覆盖率。
故障排除
- 解决错误:代码生成不支持函数
对不支持的 MATLAB 函数的代码生成失败进行故障排除。 - 解决问题:变量在使用前必须完全定义
如果变量(包括结构体字段和类属性)在使用前未定义,则需要对代码生成错误进行故障排除。 - 解决问题:元胞数组元素在使用前必须完全定义
对元胞数组元素在使用前未定义导致的代码生成错误进行故障排除。