3-8 從獨立? C 程?來呼?? MATLAB 引?

ڭ̰FiHg MATLAB өIs C {yҼg MATLAB Ƥ~A]iHϦVާ@A]NOgѿWߪ C {өIs MATLAB Aرpq`oͦbAwg@Ӥj C {XAOiQnϥ MATLAB S\ӧYǯSwu@AҦpG

Hint
  • pGAQN@ӿWߪ MATLAB ε{ഫWiɡ]Standalone ExecutableAYݭnw MATLAB YiB@ .exe ɮס^AAݭnOAΩMATLAB 5.x M 6.x uRuntime ServervucANiHNA MATLAB ε{]ݭnROCJ MATLAB ε{^ন .exe ɮסA]tҦϧܥ\AiHSw MATLAB LHϥΡC
  • b MATLAB 7.xAuRuntime Servervucwg_sbA\wghuMATLAB CompilervucFC

Aڭ̨Ӭݤ@²檺dҡA\Ob C {X̭Ұ MATLAB ð@ M ɮ plotSine.mAPɨqX whos O᪺TAC dҵ{XO plotViaMatlab01.cC]Awg]wnA C sĶ]ШĤG`^AӥB]AΪO Microsoft VC 10.0Aڭ̥ib MATLAB JUCROHsĶ C {XG

Example 1: 03-ε{/matlabEngine01.moptsFile = [matlabroot '\bin\win64\mexopts\msvc100engmatopts.bat']; mex('-f', optsFile, 'plotViaMatlab01.c'); % isĶ !plotViaMatlab01 % յ{

Hint
  • pGA@~tάO 32-bit WindowsAбNWzdҤ "win64" אּ "win32".
  • pGAϥΪsĶO Microsoft VC 8.0AбNWzdҤ "msvc100engmatopts.bat" אּ "msvc80engmatopts.bat"A̦C

Y@L~AN| plotViaMatlab01.exeAA榹{XA|Ұʥt@ MATLABAð plotSine.mAeXUCϧΡG

PɸX@ӰTAH MATLAB b whos O᪺XTG

Pɤu@C]|ܤ@ MATLAB ϥܡAN MATLAB engine b椤CAUTuTwvAMATLAB engine ]N۵FC

plotViaMatlab01.c epUG

lɡ]03-ε{/plotViaMatlab01.c^G]ǦϰUYi^
/* dһpbLnҤUA C {өIs MATLAB  */
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <direct.h>
#include "mex.h"
#include "engine.h"

#define BUFSIZE 256

int PASCAL WinMain (HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR     lpszCmdLine,
                    int       nCmdShow){
	char buffer[BUFSIZE];
	mxArray *app;
	Engine *ep;

	/* Ұ MATLAB  */
	if (!(ep = engOpen(NULL))){		// ͤ@ MATLAB 
		MessageBox ((HWND)NULL, (LPSTR)"Can't start MATLAB engine",
			(LPSTR)"plotViaMatlab01.c", MB_OK);
		exit(-1);
	}

	/* ؿð plotSine.m */
	_getcwd(buffer, BUFSIZE);		// N{ҦbؿsJr buffer
	app = mxCreateString(buffer);		//  MATLAB rܼ app
	engPutVariable(ep, "appDir", app);	// Nrܼ app ܤJu@Ŷܼ appDir
	engEvalString(ep, "cd(appDir)");	// N MATLAB u@ؿܦr appDir ҫwؿ
	engEvalString(ep, "plotSine");		// PؿU plotSine.m
	
	/* o MATLAB XT */
	engOutputBuffer(ep, buffer, BUFSIZE);	// ]w buffer iH MATLAB XT
	engEvalString(ep, "whos");		// b MATLAB  whos O
	MessageBox((HWND)NULL, (LPSTR)buffer, (LPSTR) "MATLAB - whos", MB_OK);		//  buffer e

	engClose(ep);				// ̫ MATLAB 
	return(0);
}

ڭ̤wgNAHѪ覡b{XCDnIiHpUC

  1. ڭ̭nͤ@ MATLAB EngineA~gѳoӪҰ MATLAB HΩM MATLAB qAoBiHgѤUC{XG
    Engine *ep=engOpen(NULL);
  2. AӰ]IsMATLAB{ plotSine.m PIs C {ObP@ӥؿAڭ̥N MATLAB 즹ؿA@kpUG
    • N{ҦbؿsJr bufferG_getcwd(buffer, BUFSIZE );
    • MATLAB rܼ appGapp = mxCreateString(buffer);
    • Nrܼ app ܤJu@Ŷܼ appDirGengPutVariable(ep, "appDir", app);
    • N MATLAB u@ؿܦr appDir ҫwؿGengEvalString(ep, "cd(appDir)");
  3. ̫NNڭ̷QnIs MATLAB { plotSine.m ǤJAYij\iG
  4. engEvalString(ep, "plotSine");
  5. ۧڭ̭n MATLAB b whos ұo쪺XTA@kpUG
    • ]w buffer iH MATLAB XTGengOutputBuffer(ep, buffer, BUFSIZE);
    • b MATLAB engine whos OGengEvalString(ep, "whos");
    • buffer eGMessageBox ((HWND)NULL, (LPSTR)buffer, (LPSTR) "MATLAB - whos", MB_OK);
  6. ̫ MATLAB engineGengClose(ep);

bWz MATLAB dҵ{ApGoͽsĶ~AiOAҨϥΪsĶO Microsoft VC 10.0AɭnNsĶѼ optsFile 令AXAҥΪsĶA`ΪsĶѼɮץiHܦpUG

Example 2: 03-ε{/showOpsFile.m% pGA@~tάO 32-bit WindowsAбNUC "win64" אּ "win32". dir([matlabroot '\bin\win64\mexopts\*engmatopts.bat']) intelc11msvs2008engmatopts.bat msvc100freeengmatopts.bat intelf11msvs2008engmatopts.bat msvc80engmatopts.bat intelf11msvs2008shellengmatopts.bat msvc90engmatopts.bat msvc100engmatopts.bat

ѤWzdҥiHݥXAsĶѼɮצWٳŦX *engmatopts.bat 榡AھڧAϥΪsĶANiHsĶѼɡC

Hint
YbsĶΰL{A٦L~TAiH`q޳N䴩AJrHijMA}OGhttp://www.mathworks.com/support

bUoӽdҤAڭ̦b C {XбN MATLAB ROe MATLAB AӤΩIs@ӥ~ M ɮסCsĶΰ檺dҦpUG

Example 3: 03-ε{/matlabEngine02.moptsFile = [matlabroot '\bin\win64\mexopts\msvc100engmatopts.bat']; mex('-f', optsFile, 'plotViaMatlab02.c'); % isĶ !plotViaMatlab02 % յ{

plotViaMatlab02.exe AiHeXuۥѸ骺ZPɶYϡvApUG

plotViaMatlab02.c {XpUG

lɡ]03-ε{/plotViaMatlab02.c^G]ǦϰUYi^
/* dһpbLnҤUA C {өIs MATLAB  */
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "engine.h"

int PASCAL WinMain (HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR     lpszCmdLine,
                    int       nCmdShow){
	Engine *ep;
	mxArray *T;
	double time[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

	/* Ұ MATLAB  */
	if (!(ep = engOpen(NULL))) {
		MessageBox ((HWND)NULL, (LPSTR)"Can't start MATLAB engine", 
			(LPSTR)"plotViaMatlab02.c", MB_OK);
		exit(-1);
	}
	
	T = mxCreateDoubleMatrix(1, 10, mxREAL);	// ͤ@ MATLAB ܼ T
	memcpy((char *)mxGetPr(T), (char *)time, 10*sizeof(double));	// N time ȫ T
	engPutVariable(ep, "T", T);					// N T Ȱe MATLAB u@Ŷܼ T

	/* ۥѸ骺첾ɶơGdistance = (1/2)g.*t.^2 */
	engEvalString(ep, "D = .5.*(-9.8).*T.^2;");			// Nre MATLAB h
	engEvalString(ep, "plot(T, D, 'o-');");				// eX浲G
	engEvalString(ep, "title('ۥѸ骺ZPɶY');");
	engEvalString(ep, "xlabel('ɶ ()');");
	engEvalString(ep, "ylabel('Z ()');");

	mxDestroyArray(T);			//  MATLAB ܼ T
	engClose(ep);				// ̫ MATLAB 
	return(0);
}

bWzdҤAHѪ覡gbWz{XAbAحzC

i@Bpϥ C Is MATLAB Aid\ MATLAB Helpdesk External Interfaces/API `uCalling MATLAB from C and Fortran ProgramsvAԲӪСC


MATLAB{]pGig