Return values from a C++ MEX file to MATLAB

微笑、不失礼 提交于 2019-12-02 10:07:17

To return an integer to Matlab you can follow the instructions of the link you posted but modifying it a bit so integers are returned.

void mexFunction(int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[])
{
    // Create a 1-by-1 real integer. 
    // Integer classes are mxINT32_CLASS, mxINT16_CLASS, mxINT8_CLASS
    // depending on what you want.
    plhs[0] = mxCreateNumericMatrix(1, 1, mxINT16_CLASS, mxREAL);


    // fill in plhs[0] to contain the same as whatever you want 
    // remember that you may want to change your variabel class here depending on your above flag to *short int* or something else.
    int* data = (int*) mxGetData(plhs[0]); 
    // This asigns data the same memory address as plhs[0]. the return value

    data[0]=_tmain(0,0,0); //or data[0]=anyFunctionThatReturnsInt();
    return;

}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!