从 C 调用 MATLAB
使用
mxArray 从 C 程序中运行 MATLAB® 代码引擎应用程序是程序,它们允许您从自己的 C/C++ 程序调用 MATLAB,从而将 MATLAB 用作计算引擎。要编译 C 引擎应用程序,请调用 mex 函数。
注意
推荐使用 MATLAB Engine API for C++ 而不是 Engine API for C。MATLAB Engine API for C++ 包含用于编写引擎应用程序的现代 C++ 功能。有关详细信息,请参阅从 C++ 调用 MATLAB。目前没有删除 Engine API for C 的计划。
引擎应用程序需要已安装版本的 MATLAB;您无法在只有 MATLAB Runtime 的机器上运行 MATLAB Engine。
要编译 C Engine 应用程序,您需要:
编写 C 源代码的经验。您可以使用 Engine API for C 和 C Matrix API 函数创建 C 源代码文件。
MATLAB 支持的编译器。有关支持的编译器的最新列表,请参阅支持和兼容的编译器。
使用具有
-client engine选项的mex编译脚本。
Engine API for C
Engine | MATLAB 引擎的类型 |
engOpen | Start MATLAB engine session |
engOpenSingleUse | Start MATLAB engine session for single, nonshared use |
engClose | Quit MATLAB engine session |
engEvalString | Evaluate expression in string |
engGetVariable | Copy variable from MATLAB engine workspace |
engPutVariable | Put variable into MATLAB engine workspace |
engGetVisible | Determine visibility of MATLAB engine session |
engSetVisible | Show or hide MATLAB engine session |
engOutputBuffer | Specify buffer for MATLAB output |
主题
要求
- Requirements to Build Engine C Applications
Install and configure a compiler and run-time environment so you can build C applications to call MATLAB. - Set Up C Development Environment
Set up a development environment to write C applications that call MATLAB functions.
编写引擎应用程序
- 适用于 C 和 Fortran 的 MATLAB Engine API
使用 MATLAB 作为计算引擎,从您自己的 C 和 Fortran 程序中调用 MATLAB。 - 从 C 应用程序中调用 MATLAB 函数
创建一个 C 引擎应用程序engdemo.c。 - Attach to Existing MATLAB Sessions
This example shows how to attach an engine program to a MATLAB session on a Windows® platform that is already running. - Callbacks in Applications
Design user interface callbacks to be evaluated in the context of the base workspace.
编译和运行引擎应用程序
- Build and Run C Engine Application on Windows
This example shows how to verify the build process on a Windows platform. - Build and Run C Engine Application on macOS
This example shows how to verify the build process on a macOS platform. - Build and Run C Engine Application on Linux
This example shows how to verify the build process on a Linux® platform.
疑难解答
当 MATLAB 引擎无法运行时该怎么办。
Debug MATLAB Function Called by C Engine
How to verify MATLAB functions used in engine applications.
Some MATLAB functions that interact with the user are not supported in engine applications.
MATLAB libraries are not thread-safe.