I wrote a C++ program that uses Matlab Engine C++ API . The program builds fine, but when I run it, the call to startMATLAB() results in a "The specified module could not be found" message & an exception. Any suggestions? Thank you!

2 views (last 30 days)
#include "MatlabEngine.hpp"
#include iostream
#include string
#include vector
#include "MatlabDataArray.hpp"
void main(int argc, char** argv) {
//Start a MATLAB session and get a unique pointer to the instance.
std::cout << "testing MatlabAPI" << std::endl;
//Define a vector of MATLAB data arrays for the input arguments to the MATLAB function.Each argument is an array in the vector.
// Create a vector of MATLAB data arrays for arguments
std::vector<double> args({ 4, 8, 6, -1, -2, -3});
std::cout << args[0] << std::endl;
// start matlab engine
std::unique_ptr<matlab::engine::MATLABEngine> matlabPtr = matlab::engine::startMATLAB();
std::cout << "startMATLAB called" << std::endl;
//Create a MATLAB data array factory to construct the data types used by the matlab::engine::MATLABEngine member functions.
matlab::data::ArrayFactory factory;
std::cout << " create factory" << std::endl;
std::string line;
std::cout << "Press ENTER to try...";
std::getline(std::cin, line);
matlab::data::TypedArray<double> inputArray = factory.createArray({ 2, 3 }, args.cbegin(), args.cend());
//std::cout << inputArray[0][0] << std::endl;
}

Answers (1)

Sreeram
Sreeram on 19 Jun 2025
Hi Mantalena,
I ran into the same issue while working with the MATLAB Engine C++ API - everything compiled fine, but "startMATLAB" kept throwing the error "The specified module could not be found".
In my case, it turned out to be a problem with the environment not being able to locate the required DLLs at runtime, even though the build was successful.
What finally worked for me was reinstalling MATLAB and then carefully following the steps outlined in this post:
Once that was done, everything started working normally. Might be worth a look if the issue’s the same.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!