Deploy MATLAB Function That Accepts Struct Array as Input Argument to Java Application
This example shows how to package a MATLAB® function that accepts a struct array as input and deploy it to a Java® application. The workflow is supported on Windows®, Linux®, and macOS systems. This example uses a workflow based on Windows.
Prerequisites
Create a new work folder that is visible to the MATLAB search path. This example uses a folder named
work.Verify that you have set up a Java development environment. For details, see Set Up Java Development Environment.
Verify that you have met all of the MATLAB Compiler SDK™ Java target requirements. For details, see MATLAB Compiler SDK Java Target Requirements.
End users must have an installation of MATLAB Runtime to run the application. For details, see Download and Install MATLAB Runtime.
For testing purposes, you can use an installation of MATLAB instead of MATLAB Runtime.
Data Management
To exchange data between the deployed MATLAB code and the Java application, use the MATLAB Data API for Java. This API is also used by MATLAB Engine. For details, see Java Data Type Conversions.
Create MATLAB Function
Create a MATLAB file named analyzeData.m with the following
code:
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
Test the MATLAB function at the command prompt.
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.9400Create Java Package Using compiler.build.javaPackage
Use the compiler.build.javaPackage function to generate a
code archive (.ctf file) from the MATLAB function.
buildResults = compiler.build.javaPackage( ... "analyzeData.m",... Interface="matlab-data",... Verbose="on", OutputDir=".\output",.... PackageName="com.example.matlabfunction")
Integrate MATLAB Code into Java Application
Use Java Command Line API to Build Application
Create a Java driver application named
AnalyzeDataExample.javawith the following code:The resulting directory structure after creating this file is as follows:
work/ ├── AnalyzeDataExample.java └── output/ ├── matlabfunction.ctf └── ...Compile your Java code by setting the classpath to include
matlabruntime.jar.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.javaRun your Java application by setting the classpath to include
matlabruntime.jarand by configuring access to the native runtime libraries. For details, see Set MATLAB Library Paths for Testing Deployed Applications.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
Running a Java application without setting the environment variables to provide access to the native runtime libraries from MATLAB or MATLAB Runtime results in a
java.lang.UnsatisfiedLinkError.Note
When testing applications on a machine with a full MATLAB installation, you can set up native library access using the MATLAB installation.
In a deployment scenario where MATLAB Runtime is used, you must explicitly specify the location of the native libraries within an installation of MATLAB Runtime. This distinction ensures that deployed applications run independently of a full MATLAB installation.
For details, see:
See Also
compiler.build.javaPackage | compiler.build.JavaPackageOptions | com.mathworks.runtime.MatlabRuntime
