## 14-3 ?¢ç??±äº«ç¨‹å?åº?

Example 1: 14-matlab½sÄ¶¾¹/myMatInv.mfunction invMat = myMatInv(mat) %myMatInv: Generate the inverse matrix of the given matrix % Roger Jang, 20080211 invMat=inv(mat);

Example 2: 14-matlab½sÄ¶¾¹/myMatMultiply.mfunction out = myMatMultiply(mat1, mat2) %myMatEig: Generate the product of two given matrices % Roger Jang, 20080211 out=mat1*mat2;

mcc -B csharedlib:myMatLib myMatInv.m myMatMultiply.m -d sharedLibrary

• out=myMatInv(in)->mlfMyMatInv(1, &out, in);
• out=myMatMultiply(in1, in2)->mlfMyMatMultiply(1, &out, in1, in2);

/*=======================================================
* Roger Jang, 20080211, 20120812
*=======================================================*/

#include <stdio.h>
/* ¾É¤J MCR ¼ÐÀYÀÉ¥H¤Î¡]¥Ñ MATLAB ½sÄ¶¾¹²£¥Í¡^¯S©wÀ³¥Î¬ÛÃöªº¼ÐÀYÀÉ */
#include "myMatLib.h"

/* Åã¥ÜC¸ê®Æ«¬ºA¬° mxArray ªºÂù­¿ºë·Ç°}¦C */
void matDisplay(const mxArray* in){
int i, j;
int rowNum = mxGetM(in);	/* ¾î¦C­Ó¼Æ */
int colNum = mxGetN(in);	/* ª½¦æ­Ó¼Æ */
double *data = mxGetPr(in);	/* «ü¨ì¿é¤J°}¦C¤¤ªºÂù­¿ºë·Ç¸ê®Æ */

/* ¦L¥X¸ê®Æ */
for(i=0; i<colNum; i++){
for(j=0; j<rowNum; j++)
printf("%4.2f\t", data[j*colNum+i]);
printf("\n");
}
printf("\n");
}

/* ¥D­n¤u§@¨ç¦¡ */
void *mainFunction(int *errorCode){
mxArray *in1, *in2;	/* ©w¸q°e¨ìµ{¦¡®w¨ç¦¡ªº¿é¤J¯x°} */
mxArray *out = NULL;	/* ©w¸qµ{¦¡®w¨ç¦¡ªº¿é¥X¯x°} */
double data[] = {1, 3, 5, 2, 4, 6, 0, 8, 9};	/* ´ú¸Õ¥Îªº¸ê®Æ */

if(!mclInitializeApplication(NULL, 0)){
*errorCode=-1;
return;
}
if (!myMatLibInitialize()){
*errorCode=-2;
return;
}

/* ³Ð³y¿é¤J¯x°} */
in1 = mxCreateDoubleMatrix(3, 3, mxREAL);
in2 = mxCreateDoubleMatrix(3, 3, mxREAL);
memcpy(mxGetPr(in1), data, 9*sizeof(double));	/* ±N data ¤¤ªº¸ê®Æ«þ¨©¦Ü in1 */
memcpy(mxGetPr(in2), data, 9*sizeof(double));	/* ±N data ¤¤ªº¸ê®Æ«þ¨©¦Ü in2 */

/* ©I¥s¥Ñ MATLAB ½sÄ¶¾¹²£¥Íªºµ{¦¡®w myMatLib */
/* ­pºâ¯x°}¬Û­¼ */
mlfMyMatMultiply(1, &out, in1, in2);
printf("¯x°}¬Û­¼ªºµ²ªG¡G\n"); matDisplay(out);
mxDestroyArray(out); out=NULL;	/* ¦¬¦^°t¸mµ¹ out ªº°O¾ÐÅé */
/* ­pºâ¤Ï¯x°} */
mlfMyMatInv(1, &out, in1);
printf("­pºâ¤Ï¯x°}ªºµ²ªG¡G\n"); matDisplay(out);
mxDestroyArray(out); out=NULL;	/* ¦¬¦^°t¸mµ¹ out ªº°O¾ÐÅé */

/* ¦¬¦^°t¸mµ¹ in1 ©M in2 ªº°O¾ÐÅé */
mxDestroyArray(in1); in1=NULL;
mxDestroyArray(in2); in2=NULL;

myMatLibTerminate();
mclTerminateApplication();
}

/* ¥Dµ{¦¡ */
int main(){
int errorCode=0;
mainFunction(&errorCode);
return(errorCode);
}

mbuild -IsharedLibrary myMatLibMain.c sharedLibrary/myMatLib.lib

¨ä¤¤ -IsharedLibrary ­t³d§iª¾ mbuild ¨ì sharedLibrary ¥h·j´M¼ÐÀYÀÉ myMatLib.h¡A¦Ó sharedLibrary/myMatLib.lib «h¬O­t³d¸ü©ú sharedLibrary/myMatLib.dll ªº¨Ï¥Î¤è¦¡¡C°õ¦æ§¹¤W­z«ü¥O«á¡A§Y¥i²£¥Í¿W¥ßÀ³¥Îµ{¦¡ myMatLibMain.exe¡A¦ý¦b¨Ï¥Î¦¹¥i°õ¦æÀÉ¤§«e¡A§Ú­Ì¥²¶·Åý§@·~¨t²Îª¾¹D myMatLib.dll ªº¦ì¸m¡A§_«h myMatLabMain.exe µLªk¨Ï¥Î¦¹¦@¨Éµ{¦¡®w¡A¦³¨âºØ¤è¦¡¥i¥H¹F¨ì¦¹¥Øªº¡G

1. ±N sharedLibrary/myMatLib.dll «þ¨©¨ì»P myMatLibMain.exe ¦P¤@¼h¥Ø¿ý¡]°²³]¬O¥Ø«e¥Ø¿ý¡^¡A¹ïÀ³ªº DOS «ü¥O¬O¡G copy /y sharedLibrary/myMatLib.dll .
2. ±N sharedLibrary ¥[¨ì DOS ªº·j´M¸ô®|¤§¤¤¡C set path=%path%;sharedLibrary