1 . BOOL CMatlab7dllDlg::OnInitDialog() ...{ CDialog::OnInitDialog(); …………… // TODO: Add extra initialization here /**//* Call the mclInitializeApplication routine. Make sure that the application * was initialized properly by checking the return status. This initialization * has to be done before calling any MATLAB API's or MATLAB Compiler generated * shared library functions. */ if( !mclInitializeApplication(NULL,0) ) ...{ AfxMessageBox( "Could not initialize the application."); exit(1); } /**//* Call the library intialization routine and make sure that the * library was initialized properly. */ if (!libmatrixInitialize()) ...{ AfxMessageBox("Could not initialize the library."); exit(1); } return TRUE; // return TRUE unless you set the focus to a control } 2 . void CMatlab7dllDlg::OnDestroy() ...{ CDialog::OnDestroy(); /**//* Call the library termination routine */ libmatrixTerminate(); mclTerminateApplication(); } 3 . void CMatlab7dllDlg::OnRUN() ...{ CString str; mxArray *in1, *in2; /**//* Define input parameters */ mxArray *out = NULL;/**//* and output parameters to be passed to the library functions */ double data[] =...{1,2,3,4,5,6,7,8,9}; /**//* Create the input data */ in1 = mxCreateDoubleMatrix(3,3,mxREAL); in2 = mxCreateDoubleMatrix(3,3,mxREAL); memcpy(mxGetPr(in1), data, 9*sizeof(double)); memcpy(mxGetPr(in2), data, 9*sizeof(double)); /**//* Call the library function */ mlfAddmatrix(1, &out, in1, in2); /**//* Display the return value of the library function */ str="The value of added matrix is: "; str = str + Display(out); AfxMessageBox(str); /**//* Destroy the return value since this varaible will be resued in * the next function call. Since we are going to reuse the variable, * we have to set it to NULL. Refer to MATLAB Compiler documentation * for more information on this. */ mxDestroyArray(out); out=0; mlfMultiplymatrix(1, &out, in1, in2); str ="The value of the multiplied matrix is: "; str = str+Display(out); AfxMessageBox(str); mxDestroyArray(out); out=0; mlfEigmatrix(1, &out, in1); str ="The Eigen value of the first matrix is: "; str = str+Display(out); AfxMessageBox(str); mxDestroyArray(out); out=0; /**//* Free the memory created */ mxDestroyArray(in1); in1=0; mxDestroyArray(in2); in2 =0; AfxMessageBox("OK, Finished!"); } 4 . CString CMatlab7dllDlg::Display(const mxArray *in) ...{ CString str, strout=""; int i=0, j=0; /**//* loop index variables */ int r=0, c=0; /**//* variables to store the row and column length of the matrix */ double*data; /**//* variable to point to the double data stored within the mxArray */ /**//* Get the size of the matrix */ r = mxGetM(in); c = mxGetN(in); /**//* Get a pointer to the double data in mxArray */ data = mxGetPr(in); /**//* Loop through the data and display the same in matrix format */ for( i =0; i < c; i++ )...{ for( j =0; j < r; j++)...{ str.Format("%4.2f ",data[i*c+j]); strout = strout+str; } strout = strout+""; } strout = strout +""; return strout; }
5 .附m文件: 1 )addmatrix.m function a = addmatrix(a1, a2) %ADDMATRIX Add two matrices % Copyright 2003 The MathWorks, Inc. a = a1 + a2;
2) multiplymatrix.m function m = multiplymatrix(a1, a2) %MULTIPLYMATRIX Multiplies two matrices % Copyright 2003 The MathWorks, Inc. m = a1*a2;
3) eigmatrix.m function e = eigmatrix(a1) %EIGMATRIX Returns the eigen value of the given matrix % Copyright 2003 The MathWorks, Inc. e = eig(a1);