How to use object arrays in MEX
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello everybody!!
I would appreciate any help on my questions below.
I have simple user-defined class, which is actually a wrapper around MEX library. To support vectorized operations with objects of the class effectively I need to work with arrays of objects on C/C++ level in MEX. In relation with this I have several questions:
1. How can I create array of objects of the class inside MEX library?
2. How can I access each individual object of array of objects supplied by user to MEX-function.
I stuck on these issues and I would be grateful for any advice.
Class is NOT handle based, it looks like (bare minimum):
classdef abc
properties (SetAccess = public)
a
end % properties
function obj = abc(x)
if nargin == 0
obj.a = 0;
elseif nargin == 1
obj.a = x;
end
end % constructor
end % classdef
0 commentaires
Réponse acceptée
James Tursa
le 4 Avr 2011
You can do this with a helper m-file:
% CreateClassArray.m helper file for mxCreateClassArray mex function
function class_array = CreateClassArray(dims,classname)
estring = 'class_array(';
for k=1:numel(dims);
estring = [estring '1:' num2str(dims(k)) ','];
end
estring(end) = ')';
estring = [estring '=' classname ';'];
eval(estring);
end
And then you can use this function in your mex routine:
/* Create a class array with default class element entries */
mxArray *mxCreateClassArray(mwSize ndim, const mwSize *dims, const char *classname)
{
mxArray *rhs[2];
mxArray *mx, *mxtrap;
mwSize i;
double *pr;
rhs[0] = mxCreateDoubleMatrix(1, ndim, mxREAL);
pr = mxGetPr(rhs[0]);
for( i=0; i<ndim; i++ ) {
pr[i] = dims[i];
}
rhs[1] = mxCreateString(classname);
if( mxtrap = mexCallMATLABWithTrap(1,&mx,2,rhs,"CreateClassArray") ) {
mxDestroyArray(mxtrap);
mx = NULL;
}
mxDestroyArray(rhs[1]);
mxDestroyArray(rhs[0]);
return mx;
}
Caution: The above is bare bones with NO ARGUMENT CHECKING. I will clean this up with more comments and put in argument checking and post to the FEX in the near future. Also, the above assumes R2008b or later. For R2008a there is no mexCallMATLABWithTrap function in the API. In that case download the FEX submission noted below for mxGetPropertyPtr and there is a replacement mexCallMATLABWithTrap function included.
You might also be interested in this FEX submission for getting the mxArray pointers to class element properties:
0 commentaires
Plus de réponses (1)
Voir également
Catégories
En savoir plus sur Write C Functions Callable from MATLAB (MEX Files) dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!