How to find size of CellArray in mex c++?

조회 수: 17 (최근 30일)
Andrew Rhodes
Andrew Rhodes 2018년 10월 17일
편집: Zehui Lu 2020년 3월 31일
I am attempting to write a c++ program to mex in Matlab. I am passing in a cell array that I need to find the size of. Here is a MWE called myMatlabFunction.cpp
#include "mex.hpp"
#include "mexAdapter.hpp"
using namespace matlab::data;
using matlab::mex::ArgumentList;
class MexFunction : public matlab::mex::Function {
public:
void operator()(ArgumentList outputs, ArgumentList inputs) {
CellArray myCell = std::move(inputs[0]);
// What goes here to find number of cells?
outputs[0] = myCell;
}
};
And the corresponding Matlab command would be
% Initialize cell
mcell = cell(3,1);
mcell{1,1} = ones(1,3);
mcell{2,1} = ones(1,2);
mcell{3,1} = ones(1,4);
% call c++ function
out = myMatlabFunction(mcell);
Following this matlab url, I am moving the input cell array to a matlab::data::CellArray. If I write
matlab::data::TypedArrayRef<double> ref myCell[0];
then I can access all the elements in the first cell of the cell array. But how do I found out the number of cells that are there? I'm having difficulty locating documentation on how to find the size.

답변 (2개)

OCDER
OCDER 2018년 10월 17일
To get the size of the cell array, use mxGetM for number of rows, mxGetN for number of columns, or mxGetNumberOfElements for number of elements.
mwSize NRow = mxGetM(prhs[0]);
mwSize NCol = mxGetN(prhs[0]);
mwSize NElem = mxGetNumberOfElements(prhs[0]);
To get the number of elements within a cell array element, extract the cell element pointer, and then use the same functions.
mxArray *pCell = mxGetCell(prhs[0], j); //Get pointer to jth element of a cell array
mwSize NumRow = mxGetM(pCell);
mwSize NumCol = mxgetN(pCell);
mwSize NumElem = mxGetNumberOfElements(pCell);
  댓글 수: 1
Andrew Rhodes
Andrew Rhodes 2018년 10월 18일
All of the functions in your answer are referring to c code. I am trying to use the c++ mex application https://www.mathworks.com/help/matlab/cpp-mex-file-applications.html.

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


Zehui Lu
Zehui Lu 2020년 3월 31일
편집: Zehui Lu 2020년 3월 31일
According to this website, there is a function called getDimensions() under matlab::data::Array can return the size of matlab input matrix in C++ API.
Basically, what I did for matrix is the following. However, I haven't tried it on other data type. But I think the MATLAB Data API documentation for C++ API might be helpful.
//This is the input matrix
TypedArray<double> matrix = std::move(inputs[0]);
//Get the dimension
std::vector<unsigned long int> size_test = matrix.getDimensions();
//We can slice this size vector
std::cout << "test_size =" << std::endl << size_test[0] << std::endl;
std::cout << "test_size =" << std::endl << size_test[1] << std::endl;
//Get the size of this size vector
std::cout << "size of size =" << std::endl << size_test.size() << std::endl;

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by