C++ MATLAB 引擎不会输出到控制台

C++ MATLAB Engine won't output to console

我正在关注在 c++ 中使用 matlab 的 matlab 示例,来自: https://uk.mathworks.com/help/matlab/matlab_external/build-c-engine-programs.html https://uk.mathworks.com/help/matlab/matlab_external/test-your-build-environment.html

我编译并 运行 它没有问题,只是它不会打印任何东西。

我正在使用 windows,下面我将提供我正在使用的代码和 makefile(两者都有效)。 我不禁觉得这是我设置的问题,但我无法弄清楚。

代码:

#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() {
    std::cout << "Starting test\n";
    callSQRT();
    return 0;
}

制作:

test: testFeval.cpp
    mex -setup -client engine C++
    mex -client engine testFeval.cpp

问题是找到 dll,将 matlab bin 的路径添加到我的路径修复了这个问题