GRF-KYO 发表于 2006-6-1 12:23

[求助]问个弱弱的问题。(关于C语言MEX文件的编写)

<P><FONT face="Times New Roman" size=3>比如有这个代码~:<br>#include &lt;math.h&gt;<br>void timestwo(double y,double x)<br>{<br>y=2*x;<br>return;<br>}<br>是不是用MEX命令编译后会生成以下代码还是??<br>#include “mex.h”</FONT></P>
<P><FONT face="Times New Roman" size=3>void timestwo(double y[],double x[])</FONT></P>
<P><FONT face="Times New Roman" size=3>{ </FONT></P>
<P><FONT face="Times New Roman" size=3>y=2.0*x</FONT></P>
<P><FONT face="Times New Roman" size=3>}</FONT></P>
<P><FONT size=3><FONT face="Times New Roman">void mexFunction(int nlhs</FONT>,<FONT face="Times New Roman">mxArray *plhs[]</FONT>,<FONT face="Times New Roman">int nrhs</FONT>,<FONT face="Times New Roman">const mxArray *prhs[])</FONT></FONT></P>
<P><FONT face="Times New Roman" size=3>{</FONT></P>
<P><FONT size=3><FONT face="Times New Roman">double *x</FONT>,<FONT face="Times New Roman">*y</FONT>;</FONT></P>
<P><FONT size=3><FONT face="Times New Roman">int mrows</FONT>,<FONT face="Times New Roman">ncols</FONT>;</FONT></P>
<P><FONT face="Times New Roman" size=3>if(nrhs!=1)</FONT></P>
<P><FONT size=3><FONT face="Times New Roman">{</FONT></FONT></P>
<P><FONT size=3><FONT face="Times New Roman">mexErrMsgTxt(</FONT>"<FONT face="Times New Roman">One input required.</FONT>"<FONT face="Times New Roman">)</FONT>;</FONT></P>
<P><FONT size=3><FONT face="Times New Roman">}</FONT></FONT></P>
<P><FONT face="Times New Roman" size=3>else if (nlhs&gt;1)</FONT></P>
<P><FONT size=3><FONT face="Times New Roman">   mexErrMsgTxt(</FONT>"<FONT face="Times New Roman">Too many output argument</FONT>"<FONT face="Times New Roman">)</FONT>;</FONT></P>
<P><FONT size=3><FONT face="Times New Roman">mrows=mxGetM(prhs)</FONT>;</FONT></P>
<P><FONT size=3><FONT face="Times New Roman">ncols=mxGetN(prhs)</FONT>;</FONT></P>
<P><FONT size=3><FONT face="Times New Roman">   if(!mxIsDouble(prhs)||mxIsComplex(prhs)||!(mrows==1&amp;&amp;ncols==1))</FONT></FONT></P>
<P><FONT size=3><FONT face="Times New Roman">mexErrMsgTxt(</FONT>"<FONT face="Times New Roman">Input must be a noncomplex scalar double.</FONT>"<FONT face="Times New Roman">)</FONT>;</FONT></P>
<P><FONT size=3><FONT face="Times New Roman">plhs=mxCreateDoubleMatrix(mrows,ncols,mxREAL)</FONT>;</FONT></P>
<P><FONT size=3><FONT face="Times New Roman">x=mxGetPr(prhs)</FONT>;</FONT></P>
<P><FONT size=3><FONT face="Times New Roman">y=mxGetPr(plhs)</FONT>;</FONT></P>
<P><FONT face="Times New Roman" size=3>timestwo(y,x)</FONT></P>
<P><FONT face="Times New Roman" size=3>}<br>还是应该在MATLAB里写上边的代码还是怎样?然后是不是写完后保存为后缀为.c的文件~再用MEX 编译后再在MATLAB 中调用?<br></FONT></P>
[此贴子已经被cdwxg于2006-6-1 13:24:58编辑过]

wachen 发表于 2006-6-3 09:14

<P>我觉得是:<BR>在c里加入头文件.h,编写完后,在matlab里mex-setup建立编译器,然后执行你的.c文件,</P>
页: [1]
查看完整版本: [求助]问个弱弱的问题。(关于C语言MEX文件的编写)