06.08.2013 Views

内容简介作者简介 - 科学与工程计算系

内容简介作者简介 - 科学与工程计算系

内容简介作者简介 - 科学与工程计算系

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

【例 12.1.5.3-2】用 C 语言编写 MEX 源码文件,在运行中实现对 MATLAB 函数的调用,<br />

画出了<br />

−0.<br />

3t<br />

y = 1−<br />

e cos( 2t)<br />

曲线。本例演示:(A )如何在 MEX 文件中调用<br />

MATLAB 的内建指令;(B)如何在 MEX 文件中调用用户的自编 M 文件。<br />

(1)<br />

#include "mex.h"<br />

#define MAX 1000<br />

//------------------------------------------------void<br />

fill( double *pr, int *pm, int *pn, int max )<br />

{<br />

int i;<br />

*pm = max/2;<br />

*pn = 1;<br />

for (i=0; i < (*pm); i++)<br />

pr[i]=i*(4*3.14159/max);<br />

}<br />

//------------------------------------------------void<br />

mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )<br />

{<br />

int m, n, max=MAX;<br />

mxArray *rhs[1], *lhs[1];<br />

}<br />

rhs[0] = mxCreateDoubleMatrix(max, 1, mxREAL);<br />

fill(mxGetPr(rhs[0]), &m, &n, MAX);<br />

mxSetM(rhs[0], m);<br />

mxSetN(rhs[0], n);<br />

mexCallMATLAB(1, lhs, 1, rhs, "mexzzy");<br />

mexCallMATLAB(0, NULL, 1, lhs, "plot");<br />

mxDestroyArray(rhs[0]);<br />

mxDestroyArray(lhs[0]);<br />

return;<br />

(2)<br />

cd d:\mywork<br />

7

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!