Can't convert the Array to this TypedArray

15 views (last 30 days)
Hi the mathworks community,
I have a quiet anoying problem with matlab C++ engine more precisely the matlab::data::TypedArray. I have wrote my C++ and matlab codes and compiled them successfully But I run the executable I got this:
terminate called after throwing an instance of 'matlab::data::detail::ArrayException<matlab::Exception, (matlab::data::ExceptionType)9>'
what(): Can't convert the Array to this TypedArray
Aborted (core dumped)
~I think that it occurs at the first line where I use matlab::data::TypedArray dataType:~
matlab::data::TypedArray<double> weedUV = factory.createArray<double>(
{ 1, 2 }, { 117.0, 91.0 });
And I found that pretty weird since I saw tons of example using exactly the same line. I URGENTLY NEED HELP GUYS!
Here is the entire C++ code:
#include "MatlabDataArray.hpp"
#include "MatlabEngine.hpp"
#include <iostream>
void client(matlab::engine::MATLABEngine* matlabPtr) {
// Create MATLAB data array factory
matlab::data::ArrayFactory factory;
// Define the input arguments
matlab::data::TypedArray<double> weedUV = factory.createArray<double>(
{ 1, 2 }, { 117.0, 91.0 }); //weedUV[0], weedUV[1]
matlab::data::TypedArray<double> orientation = factory.createArray<double>(
{ 1, 4 }, { 0.1445, 0.784, 0.712, 0.456 }); //orientation.w, orientation.x, orientation.y, orientation.z
matlab::data::TypedArray<double> robotPosition = factory.createArray<double>(
{ 1, 2 }, { 5.947123, 2.1457 }); // robotPosition[0], robotPosition[1]
matlab::data::CharArray cameraFrame = factory.createScalar("right_camera");
//Define the projection Matrix
matlab::data::TypedArray<double> const projectionMatrix = factory.createArray<double>(
{ 3, 4 }, {
245.6983, 0.0, 318.6139, 0.0,
0.0, 253.3011, 243.9297, 0.0,
0.0, 0.0, 1.0, 0.0
});
// Pass vector containing 2 scalar args in vector
std::vector<matlab::data::Array> args({
weedUV,
orientation,
robotPosition,
projectionMatrix,
cameraFrame});
// Call the MATLAB function
std::cout << "calling the api function" << std::endl;
matlab::data::TypedArray<double> points = matlabPtr->feval(u"api", args);
double x = points[0];
double y = points[1];
// Display results
std::cout << "x: " << x << ", y: " << y << std::endl;
}
int main() {
using namespace matlab::engine;
// Start MATLAB engine synchronously
std::unique_ptr<matlab::engine::MATLABEngine> matlabPtr = matlab::engine::startMATLAB();
matlabPtr->eval(u"rosshutdown;");
matlabPtr->eval(u"rosinit;");
// Call the client function with the MATLAB engine pointer
client(matlabPtr.get());
matlabPtr->eval(u"rosshutdown;");
return 0;
}

Accepted Answer

Sanogo
Sanogo on 11 Jul 2023
After a long debugging time, I noticed that the error was finally at this line:
matlab::data::CharArray cameraFrame = factory.createScalar("right_camera");
I thought this was the good way to create a matlab string through the C++ engine cause in matlab string != CharArray. But it looks like this doesn't impact my function so this worked:
matlab::data::CharArray cameraFrame = factory.createCharArray("right_camera");

More Answers (1)

Ayush Kashyap
Ayush Kashyap on 10 Jul 2023
Hi Sanogo,
Seeing the line of code, I believe it can be an issue of datatype of size.
You may try as following:
std::vector<size_t> size = {1, 2};
std::vector<double> data = {117.0, 91.0};
matlab::data::TypedArray<double> weedUV = factory.createArray<double>(size, data ...);
Using size_t datatype for defining size should resolve the error.
If the problem still persists, do check if appropriate libraries are lined properly or not, especially the one necessary for MATLAB Engine.

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!