How to find size of CellArray in mex c++?
17 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(2 个)
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);
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;
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call C++ from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!