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

17 Ansichten (letzte 30 Tage)
Andrew Rhodes
Andrew Rhodes am 17 Okt. 2018
Bearbeitet: Zehui Lu am 31 Mär. 2020
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.

Antworten (2)

OCDER
OCDER am 17 Okt. 2018
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 Kommentar
Andrew Rhodes
Andrew Rhodes am 18 Okt. 2018
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.

Melden Sie sich an, um zu kommentieren.


Zehui Lu
Zehui Lu am 31 Mär. 2020
Bearbeitet: Zehui Lu am 31 Mär. 2020
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;

Community Treasure Hunt

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

Start Hunting!

Translated by