将接受结构体数组作为参量的 MATLAB 函数部署到 Java 应用程序中
此示例演示了如何打包一个接受结构体数组作为输入的 MATLAB® 函数,并将其部署到 Java® 应用程序中。Windows®、Linux® 和 macOS 系统支持此工作流。本示例使用基于 Windows 工作流。
前提条件
创建一个对 MATLAB 搜索路径可见的新工作文件夹。此示例使用名为
work的文件夹。请确认您已搭建好 Java 开发环境。有关详细信息,请参阅 设置 Java 开发环境。
请确认您已满足所有 MATLAB Compiler SDK™ Java 目标要求。有关详细信息,请参阅 MATLAB Compiler SDK Java 目标要求。
最终用户必须安装 MATLAB Runtime 才能运行应用程序。有关详细信息,请参阅下载并安装 MATLAB Runtime。
出于测试目的,您可以使用安装的 MATLAB 来代替 MATLAB Runtime。
数据管理
要在已部署的 MATLAB 代码与 Java 应用程序之间交换数据,请使用适用于 Java 的 MATLAB Data API。MATLAB 引擎也使用此 API。有关详细信息,请参阅 Java Data Type Conversions。
创建 MATLAB 函数
使用以下代码创建一个名为 analyzeData.m 的 MATLAB 文件:
function outputStruct = analyzeData(inputStruct) % This function takes a MATLAB struct 'inputStruct' as input, performs % statistical analysis on each numeric field, and returns a struct % 'outputStruct' containing the results of these analyses. Non-numeric % fields in the input struct are ignored. % % Inputs: % inputStruct - Struct with fields containing numeric data. % % Outputs: % outputStruct - Struct with the same fields as 'inputStruct'. Each % field is a struct with 'mean', 'std', and 'max' % of the corresponding field in 'inputStruct'. % Initialize outputStruct outputStruct = struct(); % Get field names from the input struct fields = fieldnames(inputStruct); % Loop over each field and perform analysis for i = 1:length(fields) fieldName = fields{i}; % Ensure the field contains numeric data if isnumeric(inputStruct.(fieldName)) % Calculate mean outputStruct.(fieldName).mean = mean(inputStruct.(fieldName)); % Calculate standard deviation outputStruct.(fieldName).std = std(inputStruct.(fieldName)); % Calculate max value outputStruct.(fieldName).max = max(inputStruct.(fieldName)); else warning('Field %s is not numeric and was skipped.', fieldName); end end end
在命令提示符下测试 MATLAB 函数。
data = struct(); data.temperatures = [72, 75, 69, 68, 70]; data.pressures = [30, 29.5, 30.2, 29.9, 30.1]; output = analyzeData(data) output.temperatures(:) output.pressures(:)
output =
struct with fields:
temperatures: [1×1 struct]
pressures: [1×1 struct]
ans =
struct with fields:
mean: 70.8000
std: 2.7749
max: 75
ans =
struct with fields:
mean: 29.9400使用 compiler.build.javaPackage 创建 Java 包
使用 compiler.build.javaPackage 函数,根据 MATLAB 函数生成的数据生成代码存档(.ctf 文件)。
buildResults = compiler.build.javaPackage( ... "analyzeData.m",... Interface="matlab-data",... Verbose="on", OutputDir=".\output",.... PackageName="com.example.matlabfunction")
将 MATLAB 代码集成到 Java 应用程序中
使用 Java 命令行 API 构建应用程序
使用以下代码创建一个名为
AnalyzeDataExample.java的 Java 驱动应用程序:创建该文件后,生成的目录结构如下:
work/ ├── AnalyzeDataExample.java └── output/ ├── matlabfunction.ctf └── ...通过将类路径设置为包含
matlabruntime.jar来编译您的 Java 代码。REM End-users use MATLAB Runtime path since MATLAB is not be installed. set MATLABROOT=C:\Program Files\MATLAB\R2026a javac -classpath ".;%MATLABROOT%\toolbox\javabuilder\jar\matlabruntime.jar" AnalyzeDataExample.java通过将类路径设置为包含
matlabruntime.jar,并配置对本机运行时库的访问权限,来运行您的 Java 应用程序。有关详细信息,请参阅 设置 MATLAB 库路径以测试已部署的应用程序。java -classpath ".;%MATLABROOT%\toolbox\javabuilder\jar\matlabruntime.jar" AnalyzeDataExampleField: temperatures std: 2.7749 max: 75.0000 mean: 70.8000 Field: pressures std: 0.2702 max: 30.2000 mean: 29.9400
如果在未设置环境变量以访问来自 MATLAB 或 MATLAB Runtime 的本机运行时库的情况下运行 Java 应用程序,将会导致
java.lang.UnsatisfiedLinkError。注意
在安装了完整 MATLAB 版本的计算机上测试应用程序时,您可以使用 MATLAB 安装来配置对原生库的访问。
在使用 MATLAB Runtime 的部署场景中,您必须在 MATLAB Runtime 的安装目录中明确指定本机库的位置。这种区分确保了已部署的应用程序能够独立于完整的 MATLAB 安装环境运行。
有关详细信息,请参阅:
另请参阅
compiler.build.javaPackage | compiler.build.JavaPackageOptions | com.mathworks.runtime.MatlabRuntime
