从 C 调用 MATLAB
使用
mxArray
从 C 程序中运行 MATLAB® 代码
注意
Engine API for C 中的函数支持 C Matrix API 中定义的 MATLAB mxArray
数据结构。要使用现代 C++ 功能编写应用程序,请参阅 从 C++ 调用 MATLAB。
引擎应用程序是独立的程序,它们允许您从自己的 C/C++ 程序调用 MATLAB,从而将 MATLAB 用作计算引擎。要编译引擎应用程序,请调用 mex
函数。
引擎应用程序需要已安装版本的 MATLAB;您无法在只有 MATLAB Runtime 的机器上运行 MATLAB Engine。
Engine API for C
Engine | Type for MATLAB engine |
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 |
主题
编写引擎应用程序
- 使用 mxArray 创建 C Engine 应用程序
如何开始编译 C Engine 应用程序。 - 适用于 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 that is already running. - Callbacks in Applications
Design user interface callbacks to be evaluated in the context of the base workspace.
编译和运行 Windows 应用程序
- Set Run-Time Library Path on Windows Systems
At run time, tell the operating system where the API shared libraries are by setting thePath
environment variable. - Build Windows Engine Application
This example shows how to verify the build process on Windows® platforms using the C exampleengwindemo.c
. - Run Windows Engine Application
This example shows how to run the C example,engwindemo.c
, from the Windows system prompt. - Register MATLAB as a COM Server
Register MATLAB for every session, to ensure that the current version of MATLAB is the registered version.
编译和运行 Mac 应用程序
- 在 macOS 系统上设置运行时库路径
通过设置DYLD_LIBRARY_PATH
环境变量,告诉操作系统 API 共享库的位置。 - Build Engine Application on macOS
This example shows how to verify the build process on a macOS platform. - Run Engine Application on macOS
This example shows how to run the C exampleengdemo.c
from the macOS Terminal Window.
编译和运行 Linux 应用程序
- 在 Linux 系统上设置运行时库路径
通过设置LD_LIBRARY_PATH
环境变量,告诉操作系统 API 共享库的位置。 - Build Engine Application on Linux
This example shows how to verify the build process on a Linux® platform. - Run Engine Application on Linux
This example shows how to run the C exampleengdemo.c
from the Linux system prompt.
在集成开发环境中编译
- 使用 IDE 编译引擎应用程序
关于配置您的集成开发环境来编译引擎应用程序的提示。
疑难解答
What to do when MATLAB engine does not run.
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.