本文共 1993 字,大约阅读时间需要 6 分钟。
/********************************************************************* * Demo.cpp * * This file shows the basics of setting up a mex file to work with * Matlab. This example shows how to use 2D matricies. This may * * Keep in mind: * <> Use 0-based indexing as always in C or C++ * <> Indexing is column-based as in Matlab (not row-based as in C) * <> Use linear indexing. [x*dimy+y] instead of [x][y] * * For more information, see my site: www.shawnlankton.com * by: Shawn Lankton * ********************************************************************/#include#include /* Definitions to keep compatibility with earlier versions of ML */#ifndef MWSIZE_MAXtypedef int mwSize;typedef int mwIndex;typedef int mwSignedIndex;#if (defined(_LP64) || defined(_WIN64)) && !defined(MX_COMPAT_32)/* Currently 2^48 based on hardware limitations */# define MWSIZE_MAX 281474976710655UL# define MWINDEX_MAX 281474976710655UL# define MWSINDEX_MAX 281474976710655L# define MWSINDEX_MIN -281474976710655L#else# define MWSIZE_MAX 2147483647UL# define MWINDEX_MAX 2147483647UL# define MWSINDEX_MAX 2147483647L# define MWSINDEX_MIN -2147483647L#endif#define MWSIZE_MIN 0UL#define MWINDEX_MIN 0UL#endifvoid mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){//declare variables mxArray *a_in_m, *b_in_m, *c_out_m, *d_out_m; const mwSize *dims; double *a, *b, *c, *d; int dimx, dimy, numdims; int i,j;//associate inputs a_in_m = mxDuplicateArray(prhs[0]); b_in_m = mxDuplicateArray(prhs[1]);//figure out dimensions dims = mxGetDimensions(prhs[0]); numdims = mxGetNumberOfDimensions(prhs[0]); dimy = (int)dims[0]; dimx = (int)dims[1];//associate outputs c_out_m = plhs[0] = mxCreateDoubleMatrix(dimy,dimx,mxREAL); d_out_m = plhs[1] = mxCreateDoubleMatrix(dimy,dimx,mxREAL);//associate pointers a = mxGetPr(a_in_m); b = mxGetPr(b_in_m); c = mxGetPr(c_out_m); d = mxGetPr(d_out_m);//do something for(i=0;i
转载地址:http://fzwlf.baihongyu.com/