14-3 ???享程??

ϥMATLABsĶAڭ̤]iHͦ@ɵ{w]Shared Libraries^A]tҦѨϥΪ̩wq禡AHKLWε{өIsC

ҦpA]ڭ̭nͤ@Ӧ@ɵ{wA\]tx}ۭMϯx}pAɧڭ̥iwqo MATLAB 禡AmyMatInv.m]pϯx}^ myMatMultiply.m]px}ۭ^G

Example 1: 14-matlabsĶ/myMatInv.mfunction invMat = myMatInv(mat) %myMatInv: Generate the inverse matrix of the given matrix % Roger Jang, 20080211 invMat=inv(mat);

Example 2: 14-matlabsĶ/myMatMultiply.mfunction out = myMatMultiply(mat1, mat2) %myMatEig: Generate the product of two given matrices % Roger Jang, 20080211 out=mat1*mat2;

ۧڭ̭nNo MATLAB 禡sĶ@ɵ{wA{wW٬ matLibAҨϥΪOpUG

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

ɷ|bXؿ sharedLibrary ͼƭɮסADɦWO myMatLibAoɮקYO myMatLib {wAڭ̥iHϥΤ@Wε{өIsoӵ{wҩwq禡Cѩoӵ{wOѨ MATLAB 禡sĶұoA]ڭ̥iHoӹ C 禡AOO mlfMyMatInv mlfMyMatMultiplyAYpUG

ϥΦ@ɵ{w myMatLib.dll өIso C 禡Wε{dҦpUG

lɡ]14-matlabsĶ/myMatLibMain.c^G]ǦϰUYi^
/*=======================================================
 * dҵ{iHIs MATLAB sĶҲͪ{w myMatLib
 * Roger Jang, 20080211, 20120812
 *=======================================================*/

#include <stdio.h>
/* ɤJ MCR YɥHΡ] MATLAB sĶ͡^Swά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Ǹ */
    
	/* LX */
	for(i=0; i<colNum; i++){
		for(j=0; j<rowNum; j++)
			printf("%4.2f\t", data[j*colNum+i]);
		printf("\n");
	}
	printf("\n");
}

/* Dnu@禡 */
void *mainFunction(int *errorCode){
	mxArray *in1, *in2;	/* wqe{w禡Jx} */
	mxArray *out = NULL;	/* wq{w禡Xx} */
	double data[] = {1, 3, 5, 2, 4, 6, 0, 8, 9};	/* եΪ */

	/* Is mclInitializeApplication() HT{ε{wgQTaҩl */
	if(!mclInitializeApplication(NULL, 0)){
		fprintf(stderr, "~GLkҩlε{I\n");
		*errorCode=-1;
		return;
	}
	/* Is myMatLibInitialize() HT{ MATLABsĶ{wwgQTaҩl */
	if (!myMatLibInitialize()){
		fprintf(stderr,"~GLkҩl{wI\n");
		*errorCode=-2;
		return;
	}
    
	/* гyJx} */
	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 */
    
        /* Is MATLAB sĶͪ{w myMatLib */
	/* px}ۭ */
	mlfMyMatMultiply(1, &out, in1, in2);
	printf("x}ۭGG\n"); matDisplay(out);
	mxDestroyArray(out); out=NULL;	/* ^tm out O */
	/* pϯx} */
	mlfMyMatInv(1, &out, in1);
	printf("pϯx}GG\n"); matDisplay(out);
	mxDestroyArray(out); out=NULL;	/* ^tm out O */

	/* ^tm in1 M in2 O */
	mxDestroyArray(in1); in1=NULL;
	mxDestroyArray(in2); in2=NULL;

	/* Is myMatLibTerminate() Hפε{w */
	myMatLibTerminate();
	/* Is mclTerminateApplication() Hפε{ */
	mclTerminateApplication();
}

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

YnsĶWߵ{Aib MATLAB ROJpUG

mbuild -IsharedLibrary myMatLibMain.c sharedLibrary/myMatLib.lib

䤤 -IsharedLibrary tdi mbuild sharedLibrary hjMY myMatLib.hA sharedLibrary/myMatLib.lib hOtd sharedLibrary/myMatLib.dll ϥΤ覡C槹WzOAYiͿWε{ myMatLibMain.exeAbϥΦiɤeAڭ̥@~tΪD myMatLib.dll mA_h myMatLabMain.exe LkϥΦ@ɵ{wAؤ覡iHF즹تG

  1. N sharedLibrary/myMatLib.dll P myMatLibMain.exe P@hؿ]]Oثeؿ^A DOS OOG copy /y sharedLibrary/myMatLib.dll .
  2. N sharedLibrary [ DOS jM|C set path=%path%;sharedLibrary
Wz]wAunb DOS ROUJumyMatLibMainvAYiݨ즹{X檺GC

Yڭ̱NWzBJXb@ӫOɡAiHoUCdҡG

Example 4: 14-matlabsĶ/mbuild01.mfprintf('MATLAB version=%s\n', version); if ~exist('sharedLibrary'), mkdir('sharedLibrary'); end fprintf('Creating shared library myMatLib and put it under sharedLibrary...\n'); tic; mcc -B csharedlib:myMatLib myMatInv.m myMatMultiply.m -d sharedLibrary; fprintf('Time for creating shared library = %g sec\n', toc); fprintf('Compiling myMatLibMain.c...\n'); mbuild -IsharedLibrary myMatLibMain.c sharedLibrary/myMatLib.lib fprintf('Copying myMatLib.dll to the current directory...\n'); copyfile('sharedLibrary/myMatLib.dll', '.'); fprintf('Running DOS command: myMatLibMain.exe...\n'); !myMatLibMain.exe MATLAB version=7.14.0.739 (R2012a) Creating shared library myMatLib and put it under sharedLibrary... DEMO Compiler license. The generated application will expire 30 days from today, on Sat Sep 15 20:07:18 2012. Time for creating shared library = 50.5487 sec Compiling myMatLibMain.c... Copying myMatLib.dll to the current directory... Running DOS command: myMatLibMain.exe... x}ۭGG 7.00 10.00 16.00 55.00 70.00 104.00 68.00 88.00 129.00 pϯx}GG -0.86 -1.29 1.14 0.93 0.64 -0.57 -0.14 0.29 -0.14

mcc mbuild OﶵܦhAŪ̥iѡudoc mccvΡudoc mbuildvo짹㪺uW䴩AbAحzC


MATLAB{]pGig