matlabを使用してCまたはC++プログラムを呼び出す
このようにする主な目的は、matlabインタフェースの簡潔できれいな特徴を利用することができて、大量の数学の計算方法などを使うことができて、同時にまたC言語を利用して底層の特徴に近づくことができて、プログラムの運行が速くて、本当に一挙に多くて、子供はいじめません.
次の段落は回転しています.
次の段落は回転しています.
c , mex 。Matlab c , VC,BC Watcom C 。 Matlab , mex c 。 , Matlab mex -setup, 。 , , 8 。 VC C:\PROGRAM FILES\DEVSTUDIO , :“C:\PROGRA~1” ,mex 。 , hello.c。
/*hello.c*/
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{ mexPrintf("hello,world!
");
}
hello.c C:\TEST\ , Matlab CD C:\TEST\ C:\ TEST\( , C:\TEST\ )。 :
mex hello.c
, 。 C:\TEST\
, hello, :
hello,world!
C\TEST\ , :HELLO.DLL。 , mex 。 hello.c, , mexFunction 。
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
,Matlab mex ,
nlhs:
plhs:
nrhs:
,
[a,b]=test(c,d,e)
mex test , test
2,plhs,3,prhs
:
prhs[0]=c
prhs[1]=d
prhs[2]=e
, plhs[0],plhs[1] a b, 。
,prhs[i] plhs[i] mxArray 。 mex.h , , Matlab 。 , Apiguide.pdf 。
, hello.c ,
:
//hello.c 2.0
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
int i;
i=mxGetScalar(prhs[0]);
if(i==1)
mexPrintf("hello,world!
");
else
mexPrintf(" !
");
}
, hello(1), :
hello,world!
hello(0) :
!
, hello 。 , mexPrintf( c printf ) , :mxGetScalar, :
i=mxGetScalar(prhs[0]);
"Scalar" 。 Matlab ,mxGetScalar prhs[0] mxArray ( ) C 。 double , i。 , , 。 :
//hello.c 2.1
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
int *i;
i=mxGetPr(prhs[0]);
if(i[0]==1)
mexPrintf("hello,world!
");
else
mexPrintf(" !
");
}
, mxGetPr mxArray prhs[0] double 。
, , , , ? mxGetPr , ,
。
,Matlab mxGetM mxGetN 。 , , :
//show.c 1.0
#include "mex.h"
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double *data;
int M,N;
int i,j;
data=mxGetPr(prhs[0]); //
M=mxGetM(prhs[0]); //
N=mxGetN(prhs[0]); //
for(i=0;i<M;i++)
{ for(j=0;j<N;j++)
mexPrintf("%4.3f ",data[j*M+i]);
mexPrintf("
");
}
}
, :
a=1:10;
b=[a;a+1];
show(a)
show(b)
, Matlab , 1 , C , ,Matlab b(i,j) C data data[j*M+i] 。
Matlab , mex Matlab , prhs[] 。 , mex , plhs[] 。 mxArray, Matlab :mxCreateDoubleMatrix , :
mxArray *mxCreateDoubleMatrix(int m, int n, mxComplexity ComplexFlag)
m:
n:
, mxArray , plhs[] 。 , , mxGetPr。 mxGetPr (double ) , 。 show.c ,
//reverse.c 1.0
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
double *inData;
double *outData;
int M,N;
int i,j;
inData=mxGetPr(prhs[0]);
M=mxGetM(prhs[0]);
N=mxGetN(prhs[0]);
plhs[0]=mxCreateDoubleMatrix(M,N,mxREAL);
outData=mxGetPr(plhs[0]);
for(i=0;i<M;i++)
for(j=0;j<N;j++)
outData[j*M+i]=inData[(N-1-j)*M+i];
}
,Matlab double , 、 、 , 。 mex , Apiref.pdf。
, 。 , 。 , re 、 , ,
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double *inData;
double *outData;
int M,N;
//
//
if(nrhs!=1)
mexErrMsgTxt("USAGE: b=reverse(a)
");
if(!mxIsDouble(prhs[0]))
mexErrMsgTxt("the Input Matrix must be double!
");
inData=mxGetPr(prhs[0]);
M=mxGetM(prhs[0]);
N=mxGetN(prhs[0]);
plhs[0]=mxCreateDoubleMatrix(M,N,mxREAL);
outData=mxGetPr(plhs[0]);
for(i=0;i<M;i++)
for(j=0;j<N;j++)
outData[j*M+i]=inData[(N-1-j)*M+i];
}
, :mexErrMsgTxt mxIsDouble。MexErrMsgTxt 。MxIsDouble mxArray double 。 Matlab , 。
,Matlab API , mex- mx- 。 mx- mxArray , mxIsDouble,mxCreateDoubleMatrix 。 mx Matlab , mexPrintf,mxErrMsgTxt 。 , Apiref.pdf 。
, C mex 。