|
< ><FONT size=3>创建一个<FONT face="宋体, MS Song">C</FONT>语言<FONT face="宋体, MS Song">MEX</FONT>文件,实现对<FONT face="宋体, MS Song">MATLAB</FONT>两个“单行”字符串的合并。本例演示:(<FONT face="宋体, MS Song">A</FONT>)如何根据<FONT face="宋体, MS Song">MATLAB</FONT>约定的规则编写<FONT face="宋体, MS Song">C MEX</FONT>源码;(<FONT face="宋体, MS Song">B</FONT>)如何构成该文件的调用指令;(<FONT face="宋体, MS Song">C</FONT>)如何为<FONT face="宋体, MS Song">MEX</FONT>文件编写在线帮助文件。</FONT></P>
< ><FONT size=3>(<FONT face="宋体, MS Song">1</FONT>)<B ><p></p></B></FONT></P>
< ><FONT face="Times New Roman" size=3>#include "mex.h" //<1></FONT></P>
<P ><FONT face="Times New Roman" size=3>#include "string.h" //<2></FONT></P>
<P ><p><FONT face="Times New Roman" size=3> </FONT></p></P>
<P ><FONT face="Times New Roman" size=3>//-------------------------------------------------</FONT></P>
<P ><FONT face="Times New Roman" size=3>void stringplus(char *input_buf0,char *input_buf1,char *output_buf)</FONT></P>
<P ><FONT face="Times New Roman" size=3>{</FONT></P>
<P ><FONT face="Times New Roman"><FONT size=3> strcat(output_buf,input_buf0);</FONT></FONT></P>
<P ><FONT face="Times New Roman"><FONT size=3> strcat(output_buf,input_buf1);</FONT></FONT></P>
<P ><FONT face="Times New Roman" size=3>}</FONT></P>
<P><p><FONT face="宋体, MS Song" size=3> </FONT></p></P>
<P ><FONT face="Times New Roman" size=3>//-------------------------------------------------</FONT></P>
<P ><FONT face="Times New Roman" size=3>void mexFunction(int nlhs,mxArray *plhs[], int nrhs,const mxArray *prhs[]) //<10></FONT></P>
<P ><FONT face="Times New Roman" size=3>{</FONT></P>
<P ><FONT size=3><FONT face="Times New Roman">char *input_buf0,*input_buf1,*output_buf; </FONT></FONT></P>
<P ><FONT size=3><FONT face="Times New Roman">int buflen,buflen0,buflen1,status; </FONT></FONT></P>
<P ><p><FONT face="Times New Roman" size=3> </FONT></p></P>
<P ><FONT size=3><FONT face="Times New Roman"> if (nrhs!=2) //<13></FONT></FONT></P>
<P ><FONT size=3><FONT face="Times New Roman"> mexErrMsgTxt("Two inputs inquired."); //<14></FONT></FONT></P>
<P ><FONT size=3><FONT face="Times New Roman"> else if (nlhs>1) //<15></FONT></FONT></P>
<P ><FONT size=3><FONT face="Times New Roman"> mexErrMsgTxt("Too many output arguments."); //<16></FONT></FONT></P>
<P ><p><FONT face="Times New Roman" size=3> </FONT></p></P>
<P ><FONT size=3><FONT face="Times New Roman"> if (mxIsChar(prhs[0])!=1||mxIsChar(prhs[1])!=1) //<17></FONT></FONT></P>
<P ><FONT size=3><FONT face="Times New Roman"> mexErrMsgTxt("Inputs must be a string.");</FONT></FONT></P>
<P ><FONT size=3><FONT face="Times New Roman"> if (mxGetM(prhs[0])!=1||mxGetM(prhs[1])!=1) //<19></FONT></FONT></P>
<P ><FONT size=3><FONT face="Times New Roman"> mexErrMsgTxt("Inputs must be a row vector.");</FONT></FONT></P>
<P ><FONT face="Times New Roman" size=3> </FONT></P>
<P ><FONT size=3><FONT face="Times New Roman"> buflen0=(mxGetM(prhs[0])*mxGetN(prhs[0]))+1; //<21></FONT></FONT></P>
<P ><FONT size=3><FONT face="Times New Roman"> buflen1=(mxGetM(prhs[1])*mxGetN(prhs[1]))+1; //<22></FONT></FONT></P>
<P ><FONT size=3><FONT face="Times New Roman"> buflen=buflen0+buflen1-1; </FONT></FONT></P>
<P ><p><FONT face="Times New Roman" size=3> </FONT></p></P>
<P ><FONT size=3><FONT face="Times New Roman"> input_buf0=(char *)mxCalloc(buflen0,sizeof(char));</FONT></FONT></P>
<P ><FONT face="Times New Roman" size=3> </FONT></P>
<P ><FONT size=3><FONT face="Times New Roman"> <p></p></FONT></FONT></P>
<P ><FONT size=3><FONT face="Times New Roman"> input_buf1=(char *)mxCalloc(buflen1,sizeof(char));</FONT></FONT></P>
<P ><FONT size=3><FONT face="Times New Roman"> output_buf=(char *)mxCalloc(buflen,sizeof(char));</FONT></FONT></P>
<P ><p><FONT face="Times New Roman" size=3> </FONT></p></P>
<P ><FONT size=3><FONT face="Times New Roman"> //</FONT></FONT></P>
<P ><FONT size=3><FONT face="Times New Roman"> status=mxGetString(prhs[0],input_buf0,buflen0); //<30></FONT></FONT></P>
<P ><FONT size=3><FONT face="Times New Roman"> if (status!=0) </FONT></FONT></P>
<P ><FONT size=3><FONT face="Times New Roman"> mexWarnMsgTxt("Not enough space,String is truncated.");</FONT></FONT></P>
<P ><p><FONT face="Times New Roman" size=3> </FONT></p></P>
<P ><FONT size=3><FONT face="Times New Roman"> //</FONT></FONT></P>
<P ><FONT size=3><FONT face="Times New Roman"> status=mxGetString(prhs[1],input_buf1,buflen1); //<34></FONT></FONT></P>
<P ><FONT size=3><FONT face="Times New Roman"> if (status!=0) </FONT></FONT></P>
<P ><FONT size=3><FONT face="Times New Roman"> mexWarnMsgTxt("Not enough space,String is truncated.");</FONT></FONT></P>
<P ><p><FONT face="Times New Roman" size=3> </FONT></p></P>
<P ><FONT size=3><FONT face="Times New Roman"> stringplus(input_buf0,input_buf1,output_buf); </FONT></FONT></P>
<P ><p><FONT face="Times New Roman" size=3> </FONT></p></P>
<P ><FONT size=3><FONT face="Times New Roman"> //</FONT></FONT></P>
<P ><FONT size=3><FONT face="Times New Roman"> plhs[0]=mxCreateString(output_buf); //<39></FONT></FONT></P>
<P ><FONT size=3><FONT face="Times New Roman"> return;</FONT></FONT></P>
<P ><FONT face="Times New Roman" size=3>}</FONT></P>
<P ><B><p><FONT face="Times New Roman" size=3> </FONT></p></B></P> |
|