Unable to access indices of TypedArray in MEX C++

조회 수: 6 (최근 30일)
Dibyendu Dutta
Dibyendu Dutta 2019년 7월 4일
답변: Kritika Bansal 2019년 7월 29일
I am trying to implement a simple function in MATLAB MEX C++, which will take input of 2 arrays- x and v (same length), and xq. The function needs to interpolate via 'previous' data point logic (as interpl1 MATLAB function) and output a corresponding vq.
After spending a day to eliminate all errors and making a MEX c++ file that compiles, I am now stuck with weird runtime errors.
  1. It seems that if I say: outputs[0][0] = vq; where, vq is a scalar double. Trying to call the function gives an error "Can't index into an empty array". Instead if I use outputs[0] = x; that is, only array output seems to works! I think this is because the output variable needs to be of the special MATLAB variable format, but I couldn't find any matlab::data:: scalar types that could be used here instead.
  2. Next, I am unable to use any of the standard iterators to access the individual scalar values in TypedArrays, even if I use TypedIterators. Attempting to access values via x[0],x[1]... does not work, reports error "Array index invalid" or "Can't index into an empty array".
  3. Looking at other MEX C++ examples, I found that things like the c++11 auto for (auto& elem : arrayName) { elem += 5; }, works. But from such a form one cannot extract index values. If then till indexing doesn't work I can't proceed.
Please let me know what I am doing wrong here.
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs)
{
checkArguments(outputs, inputs);
double xq = inputs[0][0];
const matlab::data::TypedArray<double> x = std::move(inputs[1]);
const matlab::data::TypedArray<double> v = std::move(inputs[2]);
xq = interpl_last_val(xq, x,v);
//outputs[0][0] = std::move(xq);
outputs[0] = x;
}
double interpl_last_val(double xq, matlab::data::TypedArray<double> x, matlab::data::TypedArray<double> v)
{
// Iterator to store the position of element found
matlab::data::TypedIterator<double> it = x.begin();
int temp;
for (it = x.begin(); it < x.end() ; it++)
{
*it += 10;
if (*it > xq)
{
break;
}
}
temp = it - x.begin();
xq = v[temp-1];
return xq;
}
  댓글 수: 1
Dibyendu Dutta
Dibyendu Dutta 2019년 7월 5일
After more tries I have implemented the function by use of TypedIterators, which bypasses the issue of unable to access data using indexes like v[index].
Sadly the use of this mex function as a part of my MATLAB simulation slows the process down instead of accelerating it, compared to the MATLAB function. Maybe it is overheads?
double interpl_last_val(double xq, matlab::data::TypedArray<double> x, matlab::data::TypedArray<double> v)
{
matlab::data::TypedIterator<double> it = x.begin();
matlab::data::TypedIterator<double> iq = v.begin();
for (auto& elem : x){
if (elem > xq)
break;
++iq;
}
return *(--iq);
}
However, I am dont know how one will then access specific data from a multi-dimensional array or cell? Is the only way to manipulate iterators by adding/substracting assuming row/column major rules?

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

답변 (1개)

Kritika Bansal
Kritika Bansal 2019년 7월 29일
Hi,
Using mex function may seem to be slow during the first run of your simulation because it also involves building the file. But on the subsequent runs, the simulation model directly uses the build file and runs the simulation faster.
As far as accessing the elements in a multi-dimensional array is considered, you need to use pointers along with the concept of row and column major rules, as rightly pointed out by you.
You can refer to the following link and the example cpp files given here for more guidance on this topic:

카테고리

Help CenterFile Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by