/*================================================================= * * MATRIX.C示例驱动程序代码,它将调用*使用MATLAB编译器创建一个共享库SDK。更多信息请参考MATLAB Compiler * SDK文档。* *版权1984 - 2017 MathWorks公司 . * *=================================================================*/ # 包括< stdio . h > / *包括MATLAB运行库头文件和库特定头文件*由MATLAB编译器生成的SDK。*/ #include "libmatrix.h" /*该函数用于显示存储在mxArray中的双精度矩阵。int run_main(int argc, const char **argv) {mxArray *in1, *in2;/* Define input parameters */ mxArray *out = NULL;/* and output parameters to pass to library functions */ double data[] = {1,2,3,4,5,6,7,8,9};/*创建输入数据*/ in1 = mxCreateDoubleMatrix(3,3,mxREAL);in2 = mxCreateDoubleMatrix (3 3 mxREAL);memcpy (mxGetPr (in),数据,9 * sizeof(双));memcpy (mxGetPr (in2),数据,9 * sizeof(双)); /* Call the library intialization routine and make sure that the * library was initialized properly. */ if (!libmatrixInitialize()){ fprintf(stderr,"Could not initialize the library.\n"); return -2; } else { /* Call the library function */ mlfAddmatrix(1, &out, in1, in2); /* Display the return value of the library function */ printf("The sum of the matrix with itself is:\n"); display(out); /* Destroy the return value since this variable will be reused in * the next function call. Since we are going to reuse the variable, * we must set it to NULL. Refer to MATLAB Compiler SDK documentation * for more information. */ mxDestroyArray(out); out=0; mlfMultiplymatrix(1, &out, in1, in2); printf("The product of the matrix with itself is:\n"); display(out); mxDestroyArray(out); out=0; mlfEigmatrix(1, &out, in1); printf("The eigenvalues of the original matrix are:\n"); display(out); mxDestroyArray(out); out=0; /* Call the library termination routine */ libmatrixTerminate(); /* Free the memory created */ mxDestroyArray(in1); in1=0; mxDestroyArray(in2); in2=0; } /* Note that you should call mclTerminateApplication at the end of * your application. */ mclTerminateApplication(); return 0; } /*DISPLAY This function will display the double matrix stored in an mxArray. * This function assumes that the mxArray passed as input contains double * array. */ void display(const mxArray* in) { size_t i=0, j=0; /* loop index variables */ size_t 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 it in matrix format */ for( i = 0; i < c; i++ ) { for( j = 0; j < r; j++) { printf("%4.2f\t",data[j*c+i]); } printf("\n"); } printf("\n"); } int main(int argc, const char ** argv) { /* 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 APIs or MATLAB Compiler SDK * generated shared library functions. */ if( !mclInitializeApplication(NULL,0) ) { fprintf(stderr, "Could not initialize the application.\n"); return -1; } return mclRunMain((mclMainFcnType)run_main, argc, argv); }