Read access violation when using setVariable from C++ with MatlabEngine
1 view (last 30 days)
Show older comments
I'm struggling with interfacing a C++ program with Matlab, I have a function a want to run in matlab to classify an image which is passed in from a real time image feed generated in C++. The code looks like this:
matlab::data::ArrayFactory factory;
matlab::data::ArrayDimensions dims = { (size_t)image.rows, (size_t)image.cols };
cv::Mat tImage;
cv::transpose(image, tImage);
matlab::data::TypedArray<uint16_t> imageArray = factory.createArray(dims, (uint16_t*)tImage.datastart, (uint16_t*)tImage.dataend);
matlabPtr->setVariable(matlab::engine::convertUTF8StringToUTF16String("image"), imageArray);
matlabPtr->eval(matlab::engine::convertUTF8StringToUTF16String("classifyBuildStage(image)"));
matlab::data::CharArray result = matlabPtr->getVariable(matlab::engine::convertUTF8StringToUTF16String("buildStage"));
It takes an OpenCV image, transposes it to make it column major order, creates a Matlab array from it then passes it in to matlab. The error occurs on 6th line with the setVariable command which throws a read access violation with the message "Exception thrown: read access violation. this was 0xFFFFFFFFFFFFFFFF.". The debugger stops on line 530 of matlab_engine_impl.hpp. As far as I know I'm following the docs for how to do this properly, but I can't find why its not working.
I'm using Matlab 2017b and Visual Studio 2017 as an IDE and compiler, I've included the needed header files (MatlabEngine.hpp, MatlabDataArray.hpp) and added the include directory, lib directory and bin directory to the relevant project/system paths.
0 Comments
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!