2

I would like to read some variable's value in Matlab from c++. I searched in Internet and found out below example in Matlab Documentation Page.

for using this example I Did below steps:

I add this include path to project : c:\program files\Matlab\r2017b\extern\include

then I Add this path Library Directory : c:\program Files\Matlab\r2017b\extern\lib\win64\microsoft

then I Add this library to project : "libMatlabEngine.lib" "libMatlabDataArray.lib"

then I placed needed DLLs beside application EXE file. then I ran the application after that application faced with access violataion error when startMATLAB() Method has been ran.

Note: I had other problem that I resolved it. but I think that problem was very strange and may be knowing that problem help you to find main reason of my problems.

problem was : when I set dll's files path in environment variables my app didn't find dlls and get "no entry point to *.dll" run time error. but when I copy dlls beside of exe, my app saw them.(I restarted VS2013 after change environment variables.)

#include "MatlabDataArray.hpp"
#include "MatlabEngine.hpp"
#include <iostream>

void callgetVars() {
    using namespace matlab::engine;

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

    // Evaluate MATLAB statement
    matlabPtr->eval(convertUTF8StringToUTF16String("[az,el,r] =       cart2sph(5,7,3);"));

   // Get the result from MATLAB
    matlab::data::TypedArray<double> result1 = matlabPtr->
        getVariable(convertUTF8StringToUTF16String("az"));
    matlab::data::TypedArray<double> result2 = matlabPtr->
        getVariable(convertUTF8StringToUTF16String("el"));
    matlab::data::TypedArray<double> result3 = matlabPtr->
        getVariable(convertUTF8StringToUTF16String("r"));

    // Display results
    std::cout << "az: " << result1[0] << std::endl;
    std::cout << "el: " << result2[0] << std::endl;
    std::cout << "r: " << result3[0] << std::endl;
}

I use vs2013 and Matlab 2017b in windows 7. Thanks for your help.

hamed
  • 471
  • 1
  • 9
  • 22

0 Answers0