필터 지우기
필터 지우기

Can't convert the Array to this TypedArray

조회 수: 38 (최근 30일)
Sanogo
Sanogo 2023년 7월 9일
편집: Sanogo 2023년 7월 11일
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;
}

채택된 답변

Sanogo
Sanogo 2023년 7월 11일
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");

추가 답변 (1개)

Ayush Kashyap
Ayush Kashyap 2023년 7월 10일
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.
  댓글 수: 1
Sanogo
Sanogo 2023년 7월 10일
편집: Sanogo 2023년 7월 11일
Thank you for the help @Ayush Kashyap!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Exchange and Mapping with C++ Applications에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by