Deploy MATLAB Function That Accepts Struct Array as Input Argument
This example shows how to package a MATLAB® function that accepts a struct array as input and deploy it to an application written in C++. The workflow is supported on Windows®, Linux®, and macOS systems.
For a discussion on how to work with MATLAB struct array based on this example, see Work with MATLAB Structure Arrays.
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 C++ development environment. For details, see Set Up C++ Development Environment. This example uses MATLAB as a C++ development environment. Therefore, verify that you have a C++ compiler installed by typing
mbuild -setup C++at the MATLAB command prompt.Verify that you have met all of the MATLAB Compiler SDK™ C++ target requirements. For details, see MATLAB Compiler SDK C++ 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 when running the C++ application.
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'. % arguments (Input) inputStruct (1,1) struct end % 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
Established MATLAB users may find the presence of an arguments block unconventional. The
arguments block lets you represent C++ data types with
an equivalent MATLAB type.
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.9400Package MATLAB Function Using compiler.build.cppSharedLibrary
Create a code archive (.ctf file) and header
(.hpp file) from the MATLAB function using the compiler.build.cppSharedLibrary function.
buildResults = compiler.build.cppSharedLibrary("analyzeData.m", ... OutputDir=".\output", Verbose="on");
The function produces a suite of files, as enumerated below, and places them
in the specified output directory. Among these, the key files
utilized during the integration process are the code archive
(.ctf file) containing the MATLAB code and the corresponding header (.hpp file).
For information on the other files, see Files Generated After Packaging MATLAB Functions.
P:\MATLAB\WORK\OUTPUT
│ GettingStarted.html
│ includedSupportPackages.txt
│ mccExcludedFiles.log
│ readme.txt
│ requiredMCRProducts.txt
│ unresolvedSymbols.txt
│
└───v2
└───generic_interface
analyzeData.ctf
analyzeDatav2.hpp
readme.txtTo finalize integration, you need the analyzeData.ctf code
archive file and the analyzeDatav2.hpp header file from the
generic_interface folder. You can view the header file
here:
Note
The generated artifacts do not include MATLAB Runtime or an installer. To create an installer using the
buildResults object, see compiler.package.installer.
Integrate MATLAB Code Archive into C++ Application
You can finalize the integration process in your preferred C++ development environment, including MATLAB or alternatives such as Microsoft® Visual Studio® on Windows. This example, however, uses MATLAB as a C++ development environment. For details, see Set Up C++ Development Environment.
To integrate the generated MATLAB code archive (.ctf file) and header
(.hpp file) into a C++ application, adhere to these guidelines:
Use a
#includedirective to incorporate the generated header file (.hppfile) in your C++ application code.Ensure the code archive (
.ctffile) is positioned in a location that the C++ executable can access.
Completing the integration step requires proficient C++ skills for writing application code. You can use the following sample C++ application code as guide when writing your own application.
In the
workfolder for this example create a new file namedStructConsoleApp.cppwith the following code.Compile and link the application by executing the
mbuildfunction at the MATLAB command prompt.mbuild -v StructConsoleApp.cpp -outdir output\bin
Handle Code Archive (.ctf file)
To ensure your C++ application can access the code archive
(.ctf file) containing MATLAB code, place the file in a location accessible to the executable.
For this example we are going to do this by setting the
CPPSHARED_BASE_CTF_PATH environment variable in the
MATLAB desktop environment.
setenv("CPPSHARED_BASE_CTF_PATH","P:\MATLAB\work\output\v2\generic_interface")
If you're using Visual Studio, see Set Environment Variables in Visual Studio.
For a complete list of code archive (.ctf file) placement
options, see Code Archive (.ctf file) Placement.
Run C++ Application
For testing purposes, you can run the application from MATLAB command prompt. This does not require a MATLAB Runtime installation.
!output\bin\StructConsoleApp.exeField: temperatures mean: 70.8000 std: 2.7749 max: 75.0000 Field: pressures mean: 29.9400 std: 0.2702 max: 30.2000
See Also
compiler.build.cppSharedLibrary
