Main Content

Integrate MATLAB Function in C++ Application

This example shows how to create, build, and run a C++ application that calls a MATLAB® function using feval function. The C++ application code uses the MATLAB Engine API for C++ and the MATLAB Data API for C++ to work with MATLAB functions and data. The example calls the mex command to build the application using MATLAB.

Prerequisites

  • Verify that you have set up a C++ development environment. For details, see Set Up C++ Development Environment.

  • Verify that you have met all of the requirements to build a C++ program using the MATLAB Engine API for C++. For details, see Requirements to Build C++ Engine Applications.

  • Verify that you have a C++ compiler installed by entering this statement at the MATLAB command prompt:

    mex -setup -client engine C++

Create C++ Application That Calls MATLAB

This example uses MATLAB as a C++ development environment. This C++ code includes the header files for the MATLAB Engine and Data APIs for C++ as well as the standard input/output stream library. The code uses the engine API to start the MATLAB session and the data API to create a four-element double array. The feval function calls the MATLAB sqrt function with the array as input. The code then displays the results of the feval function call.

For this example, save the code in a file named testFeval.cpp.

#include "MatlabDataArray.hpp"
#include "MatlabEngine.hpp"
#include <iostream>
void callSQRT() {

    using namespace matlab::engine;

    // Start MATLAB engine synchronously
    std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB();

    // Create MATLAB data array factory
    matlab::data::ArrayFactory factory;

    // Define a four-element typed array
    matlab::data::TypedArray<double> const argArray = 
        factory.createArray({ 1,4 }, { -2.0, 2.0, 6.0, 8.0 });

    // Call MATLAB sqrt function on the data array
    matlab::data::Array const results = matlabPtr->feval(u"sqrt", argArray);

    // Display results
    for (int i = 0; i < results.getNumberOfElements(); i++) {
        double a = argArray[i];
        std::complex<double> v = results[i];
        double realPart = v.real();
        double imgPart = v.imag();
        std::cout << "Square root of " << a << " is " << 
            realPart << " + " << imgPart << "i" << std::endl;
    }
}

int main() {
    callSQRT();
    return 0;
}

Build C++ Application

Compile and link the testFeval application using the mex function. This command saves the executable file testFeval.exe in the current folder.

mex -v -client engine testFeval.cpp

Run C++ Application

To run your application, be sure to first configure your run-time environment by adding the appropriate environment variable to the specified path. For more information, see Run-Time Environment.

For example, if your matlabroot on Windows® is C:\Program Files\MATLAB\R2024b, then this system prompt command sets the run-time environment variable:

set PATH=C:\Program Files\MATLAB\R2024b\extern\bin\win64;%PATH%

Then run the application.

testFeval.exe

MATLAB returns a complex array to the C++ program because one of the numbers in the data array is negative, and then the C++ program displays the results.

Square root of -2 is 0 + 1.41421i
Square root of 2 is 1.41421 + 0i
Square root of 6 is 2.44949 + 0i
Square root of 8 is 2.82843 + 0i

See Also

|

Related Topics