Handling syms in Mex Files

2 views (last 30 days)
Bogdan
Bogdan on 24 Apr 2014
Edited: James Tursa on 25 Apr 2014
Hi,
I have created a simple MEX file which takes as an input an array of symbolic objects. I would like to extract the data in C.
My C code looks like this:
mexPrintf("class id of <monoPowers> = %d\n", mxGetClassID(monoPowers));
mexPrintf("class name of <monoPowers> = %s\n", mxGetClassName(monoPowers));
mexPrintf("number of elements of <monoPowers> = %2d\n", nrOfElements);
mexPrintf("size of one element of <monoPowers> = %2d\n", sizeOfElem);
What I get is the following:
class id of <monoPowers> = 40
class name of <monoPowers> = sym
number of elements of <monoPowers> = 1
size of one element of <monoPowers> = 8
I checked the documentation and 40 corresponds to no known data type. How can one get each symbol in the array and converted in C to some known data type (for example char array)?
At the same time in matlab the array of symbolic objects has 3 elements but in C it looks like it has only one element. How is this possible?
Because the number of elements is 1 and the size of one element is 8 I assume this is pointer to something else ...
Thanks, Bogdan.

Answers (1)

James Tursa
James Tursa on 25 Apr 2014
Edited: James Tursa on 25 Apr 2014
Using this variable:
>> syms a b c
>> x = [a b c]
x =
[ a, b, c]
And this mex file (symdetails.c):
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
if( nrhs ) {
mexPrintf("ClassID = %d\n",mxGetClassID(prhs[0]));
mexPrintf("ClassName = %s\n",mxGetClassName(prhs[0]));
mexPrintf("NumberOfElements = %d\n",mxGetNumberOfElements(prhs[0]));
mexPrintf("ElementSize = %d\n",mxGetElementSize(prhs[0]));
}
}
I get this result on a 32-bit system:
>> symdetails(x)
ClassID = 74
ClassName = sym
NumberOfElements = 1
ElementSize = 4
Even though at the command line I get this:
>> numel(x)
ans =
3
>> size(x)
ans =
1 3
So it appears the sym information is held in some special format and the regular size etc functions do not apply (as you have already discovered). The size-of-one-element is probably just reporting back the size of some pointers (same guess as yours). You can convert to a string inside a mex routine via the mexCallMATLAB facility with the "char" function. But to get the individual elements, without manually parsing the string inside your mex function, will probably require multiple mexCallMATLAB calls with "subsref" or something like that (I have never done this myself). To recover the actual size of the sym array you will probably have to mexCallMATLAB with "numel" or the like.

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!