声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1933|回复: 1

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

[复制链接]
发表于 2006-6-1 12:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
<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[0]=2.0*x[0]</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[0])</FONT>;</FONT></P>
<P><FONT size=3><FONT face="Times New Roman">ncols=mxGetN(prhs[0])</FONT>;</FONT></P>
<P><FONT size=3><FONT face="Times New Roman">   if(!mxIsDouble(prhs[0])||mxIsComplex(prhs[0])||!(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[0]=mxCreateDoubleMatrix(mrows,ncols,mxREAL)</FONT>;</FONT></P>
<P><FONT size=3><FONT face="Times New Roman">x=mxGetPr(prhs[0])</FONT>;</FONT></P>
<P><FONT size=3><FONT face="Times New Roman">y=mxGetPr(plhs[0])</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编辑过]

回复
分享到:

使用道具 举报

发表于 2006-6-3 09:14 | 显示全部楼层
<P>我觉得是:<BR>在c里加入头文件.h,编写完后,在matlab里mex-setup建立编译器,然后执行你的.c文件,</P>
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-9-25 15:26 , Processed in 0.051824 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表