f



Create (Matlab) object inside C/C++ and pass it to Matlab

I'm trying to create an object inside C++ and then pass it to Matlab via en=
gPutVariable.


It seems I can create a struct

// Create a float array
mxArray *acq_data =3D mxCreateNumericMatrix(0, 0, mxSINGLE_CLASS);
float *data =3D (float *)mxCalloc(M*N, sizeof(float));
mxSetData(acq_data, data);
mxSetM(acq_data, M);
mxSetN(acq_data, N);

// Convert to struct
const char *fieldname =3D "data";
mxArray *acq_struct =3D mxCreateStructMatrix(1,1,1, &fieldname);
mxSetField(acq_struct,i,"data",acq_data);

// Convert to class ?? Does it work?
if (j=3D=3D1)
{
    const char *classname =3D "myClassTest";
    mxSetClassName(acq_struct, classname);
}
printf("j=3D%i Class=3D%s\n",j,mxGetClassName(acq_struct));

// Send to Matlab
engPutVariable(ep, "T", acq_struct);

// Execute command in Matlab
engEvalString(ep, "disp(class(T))");


However, running this indicates that Matlab still considers it a struct.

Class=3Dstruct
j =3D 0. >> struct

Class=3DmyClassTest
j =3D 1. >> struct

I have a file called myClassTest.m in the directory but it seems to be irre=
levant.

classdef myClassTest
    properties
        data;
    end
    methods
        function obj =3D myClassTest(obj)
            disp(num2str(mean(data)))
        end
    end
end


I was kind of hoping that the appearance of the object in Matlab would exec=
ute the constructor. The purpose would be to avoid engEvalString calls sinc=
e they have a high overhead - about 5ms per call regardless of the work don=
e. Not a lot but my application is calling it every 1-2ms so it's the bottl=
eneck.
0
2460acc
7/22/2015 9:20:58 AM
comp.soft-sys.matlab 211264 articles. 25 followers. lunamoonmoon (257) is leader. Post Follow

0 Replies
1383 Views

Similar Articles

[PageSpeed] 4

Reply: