1 | /*================================================================= |
---|
2 | * |
---|
3 | * MATRIXDRIVER.C Sample driver code that calls the shared |
---|
4 | * library created using MATLAB Compiler. Refer to the |
---|
5 | * documentation of MATLAB Compiler for more information on |
---|
6 | * this |
---|
7 | * |
---|
8 | * This is the wrapper C code to call a shared library created |
---|
9 | * using MATLAB Compiler. |
---|
10 | * |
---|
11 | * Copyright 1984-2007 The MathWorks, Inc. |
---|
12 | * |
---|
13 | *=================================================================*/ |
---|
14 | |
---|
15 | #include <stdio.h> |
---|
16 | |
---|
17 | /* Include the MCR header file and the library specific header file |
---|
18 | * as generated by MATLAB Compiler */ |
---|
19 | #include "libmatrix.h" |
---|
20 | |
---|
21 | /* This function is used to display a double matrix stored in an mxArray */ |
---|
22 | void display(const mxArray* in); |
---|
23 | |
---|
24 | int run_main(int argc, char **argv) |
---|
25 | { |
---|
26 | mxArray *in1, *in2; /* Define input parameters */ |
---|
27 | mxArray *out = NULL;/* and output parameters to be passed to the library functions */ |
---|
28 | |
---|
29 | double data[] = {1,2,3,4,5,6,7,8,9}; |
---|
30 | |
---|
31 | /* Call the mclInitializeApplication routine. Make sure that the application |
---|
32 | * was initialized properly by checking the return status. This initialization |
---|
33 | * has to be done before calling any MATLAB API's or MATLAB Compiler generated |
---|
34 | * shared library functions. */ |
---|
35 | if( !mclInitializeApplication(NULL,0) ) |
---|
36 | { |
---|
37 | fprintf(stderr, "Could not initialize the application.\n"); |
---|
38 | return -1; |
---|
39 | } |
---|
40 | |
---|
41 | /* Create the input data */ |
---|
42 | in1 = mxCreateDoubleMatrix(3,3,mxREAL); |
---|
43 | in2 = mxCreateDoubleMatrix(3,3,mxREAL); |
---|
44 | memcpy(mxGetPr(in1), data, 9*sizeof(double)); |
---|
45 | memcpy(mxGetPr(in2), data, 9*sizeof(double)); |
---|
46 | |
---|
47 | /* Call the library intialization routine and make sure that the |
---|
48 | * library was initialized properly. */ |
---|
49 | if (!libmatrixInitialize()){ |
---|
50 | fprintf(stderr,"Could not initialize the library.\n"); |
---|
51 | return -2; |
---|
52 | } |
---|
53 | else |
---|
54 | { |
---|
55 | /* Call the library function */ |
---|
56 | mlfAddmatrix(1, &out, in1, in2); |
---|
57 | /* Display the return value of the library function */ |
---|
58 | printf("The value of added matrix is:\n"); |
---|
59 | display(out); |
---|
60 | /* Destroy the return value since this variable will be reused in |
---|
61 | * the next function call. Since we are going to reuse the variable, |
---|
62 | * we have to set it to NULL. Refer to MATLAB Compiler documentation |
---|
63 | * for more information on this. */ |
---|
64 | mxDestroyArray(out); out=0; |
---|
65 | mlfMultiplymatrix(1, &out, in1, in2); |
---|
66 | printf("The value of the multiplied matrix is:\n"); |
---|
67 | display(out); |
---|
68 | mxDestroyArray(out); out=0; |
---|
69 | mlfEigmatrix(1, &out, in1); |
---|
70 | printf("The eigenvalues of the first matrix are:\n"); |
---|
71 | display(out); |
---|
72 | mxDestroyArray(out); out=0; |
---|
73 | |
---|
74 | /* Call the library termination routine */ |
---|
75 | libmatrixTerminate(); |
---|
76 | |
---|
77 | /* Free the memory created */ |
---|
78 | mxDestroyArray(in1); in1=0; |
---|
79 | mxDestroyArray(in2); in2 = 0; |
---|
80 | } |
---|
81 | |
---|
82 | /* Note that you should call mclTerminate application at the end of |
---|
83 | * your application. |
---|
84 | */ |
---|
85 | mclTerminateApplication(); |
---|
86 | return 0; |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | /*DISPLAY This function will display the double matrix stored in an mxArray. |
---|
91 | * This function assumes that the mxArray passed as input contains double |
---|
92 | * array. |
---|
93 | */ |
---|
94 | void display(const mxArray* in) |
---|
95 | { |
---|
96 | int i=0, j=0; /* loop index variables */ |
---|
97 | int r=0, c=0; /* variables to store the row and column length of the matrix */ |
---|
98 | double *data; /* variable to point to the double data stored within the mxArray */ |
---|
99 | |
---|
100 | /* Get the size of the matrix */ |
---|
101 | r = mxGetM(in); |
---|
102 | c = mxGetN(in); |
---|
103 | /* Get a pointer to the double data in mxArray */ |
---|
104 | data = mxGetPr(in); |
---|
105 | |
---|
106 | /* Loop through the data and display the same in matrix format */ |
---|
107 | for( i = 0; i < c; i++ ){ |
---|
108 | for( j = 0; j < r; j++){ |
---|
109 | printf("%4.2f\t",data[j*c+i]); |
---|
110 | } |
---|
111 | printf("\n"); |
---|
112 | } |
---|
113 | printf("\n"); |
---|
114 | } |
---|
115 | |
---|
116 | int main() |
---|
117 | { |
---|
118 | mclmcrInitialize(); |
---|
119 | return mclRunMain((mclMainFcnType)run_main,0,NULL); |
---|
120 | } |
---|