Title: MATLAB ??????? ??????
1MATLAB ?????????????
- ???
- jang_at_cs.nthu.edu.tw
- http//www.cs.nthu.edu.tw/jang
- ????? ????????
23-1 MATLAB??????
- ??????
- Application Program Interface
- ??API
- ??????????????
3API ???
- ???? API ??,???????
- ? MATLAB ?? C ??? Fortran ???????
- ? MATLAB ??????(Computing Engine),????? C ?
Fortran ????? MATLAB ??,??????????? - ????C?Fortran????MATLAB???MAT?
- ?????,??ActiveX Automation,?? MATLAB??ActiveX
Automation Server,???????(Excel ?
Access?)??,??MATLAB?? ActiveX Automation
Controller,??????????? - ????????,?? DDE(Dynamic Data Exchange)????????????
???
43-2 ????MATLAB???C ??????
- ??C??? MATLAB ?????
- MATLAB ?????????,??? C ??????????MATLAB? JIT
????????????,??????????????????? - ?????,????????C ???????,??????, MATLAB
????,????????????? - ??????? C ?????? MATLAB ???,???? MATLAB ????????
C ??? - ??????? C ???????
5??? C ????????
- ?????????????
- ???????(Persistent Memory),?????????,????????
MATLAB ?????????? - ???(Serial Port)????????
- Device Drivers ?
6??
- ?? pcode ??
- ??
- ???? M ??????????
- ????????????
- ??
- ??????????????
7?????? (I)
- ?? C ???????? C ??
- ? MATLAB ???????
- mex -setup
8?????? (II)
9?????? (III)
10??
- lcc
- ?Public-domain ? C ?????
- MATLAB 5.x,?? Stateflow Toolbox ??,
- MATLAB 6.x,?? MATLAB ???
- ???mex -setup?
- MATLAB 5.x ??? DOS ??,
- MATLAB 6.x ???? DOS ??,
- ?? MATLAB ??????
- ????? DOS ?????????
11?????? (IV)
- ?? ????? Visual C/C,???1??,???Enter??
- ??,???Enter??????
12???C???
- ? DOS ?????????? C ???
- ?????? C ???
- ??? C ?????????????
- ??Microsoft Visual C/C?Watcom C/C?Borland C
?
13?????? (V)
- ???? C ??????,???? C ????
- MATLAB ????? C ??? yprime.c,????
- MATLAB root\extern\examples\mex\yprime.c
- ????,????????????,??????
- gtgt cd D\matlabBook\MATLAB???????\03-??????
- gtgt copyfile(matlabroot, '\extern\examples\mex\ypr
ime.c') - ??? copyfile ?????????,???????????? yprime.c ??
14?????? (VI)
- ? MATLAB ????,?????????? C ??
- gtgt mex yprime.c
- MATLAB ??? Visual C/C(???? C ???)??
yprime.c,???????? - ??????,?????,C ?????? yprime.dll ? yprime.mexw32
???? - ??? MATLAB ???? C ??,??????????
- gtgt which yprime
- D\matlabBook\MATLAB???????\03-??????\yprime.mexw
32
15??
- ? MATLAB 7.1 ??,??????? yprime.dll,?? MATLAB
7.1(?)??,??????? yprime.mexw32?
16?????? (VII)
- ??yprime??
- gtgt yprime(1, 14)
- ans
- 2.0000 8.9685 4.0000 -1.0947
- MEX ??
- MATLAB ??????????
- ????????,???? mex ?????????????
17??MEX??????
?? MEX ??????
Sun OS 4. x mex4
HP 9000/series 700 mexhp7
Alpha mexds
SGI mexsg
SGI 64 mexsg64
IBM RS/6000 mexrs6
Linux (32-bit) mexglx
Linux x86-64 mexa64
64-bit Solaris SPARC mexs64
Microsoft Windows (32-bit) dll(MATLAB7.1?),mexwin32(MATLAB 7.1???)
Microsoft Windows (64-bit) dll(MATLAB7.1?),mexwin64(MATLAB 7.1???)
Macintosh (PPC) mexmac
Macintosh (Intel) mexmaci
183-3 MATLAB????????API
- MATLAB ???????
- MATLAB ??????????
- ??? mxArray
- ??? C ??????(Structure)
19mxArray?Structure
- MATLAB ???????
- ??
- ????
- ?????
- ???????(Sparse Matrix)
20????????
- ????????
- ??? mxArray ??,??????(Pointers)
- pr ? pi
- ??????????????
- ????????? double ?????
- ????????
- ? MATLAB ??? API ?? mxGetPr ? mxGetPi ??
- ????? MATLAB ????????
21MATLAB ???API?? - mx
- mx ???? API ??
- MATLAB ????(??? mxArray)???
- mxGetPr(??????)
- mxGetPi(??????)
- mxGetM(?????????)
- mxGetN(?????????)
- mxCreateDoubleMatrix(??????? double ???)
22MATLAB ???API?? - mex
- mex ???? API ??
- MATLAB ????????
- mexErrMsgTxt(? MATLAB ??????,??? MEX ??)
- mexCallMATLAB(?? MATLAB ????? MATLAB ?????)
- mexPrintf(? MATLAB ????)
23MATLAB ???API?? - mat
- mat ???? API ??
- ? MAT ????????
- matGetArray(? MAT ???? mxArray ??)
- matDeleteArray(? MAT ???? mxArray ??)
243-4?????? MEX ??
- ???????(????? double)?? 2
- ???????? MATLAB ???
- MATLAB root\extern\examples\refbook\timestwo.c
- ??3-1scalarx2.c
- ?timestwo.c???????,???? scalarx2.c
25??MEX???? (I)
- ? MATLAB ?,?? C ???? scalarx2.c ????
- gtgt mex scalarx2.c
- ????,??????????
- gtgt which scalarx2
- D\matlabBook\MATLAB???????\03-??????\scalarx2.dl
l
26??MEX???? (II)
- ??????
- gtgt scalarx2(8.5)
- ans
- 17
- Â
- gtgt scalarx2('String input')
- ??? Input must be a scalar.
- gtgt scalarx2(1 2 3)
- ??? Input must be a scalar.
27MEX????? (I)
- ??? scalarx2.m ?scalarx2.mexw32 ????????
- MATLAB ?? scalarx2.mexw32 ????
- ????help scalarx2?
- MATLAB??? scalarx2.m ???????
- ?? scalarx2.mexw32 ???????,????? scalarx2.m ??
28MEX????? (II)
- gtgt type scalarx2.m
- function out scalarx2(in)
- SCALARX2 A scalar version of "times two".
- This serves as an example of putting on-line
help in an - M-file, but the actual program body is in
another MEX-file - with the same major file name.
- Â
- gtgt help scalarx2
- SCALARX2 A scalar version of "times two".
- This serves as an example of putting on-line
help in an - M-file, but the actual program body is in
another MEX-file - with the same major file name.
- Â
- gtgt which scalarx2
- D\matlabBook\MATLAB???????\03-??????\scalarx2.me
xw32
293-5 ??????? MEX ????
- ???????(?????????)??2
- ??3-2arrayx2.c
- ????
- gtgt mex arrayx2.c
- ?????????
- gtgt which arrayx2
- D\matlabBook\MATLAB???????\03-??????\yprime.mexw
32
30??????-1
- ????
- gtgt arrayx2(1 2 3)
- ans
- 2 4 6
- Â
- gtgt arrayx2(1i 22i 33i 4 5 6)
- ans
- 2.0000 2.0000i 4.0000 4.0000i 6.0000
6.0000i - 8.0000 10.0000
12.0000 - Â
- gtgt arrayx2('Test string')
- ??? Input must be a double.
31??????-2 (I)
- ?????? A ? B
- ???????? pm ? pn
- ????? m ?????? n ????????????????????? p
- ???????? C
- C(i, j)??? A(, i) ??? B(, j) ???
32??
- ???????
- ??????????????????????????,???????
33??????-2 (II)
- ??3-3pairdist.c
- ????
- gtgt mex pairdist.c
34??????-2 (III)
- ????
- gtgt which pairdist
- D\matlabBook\MATLAB???????\03-??????\pairdist.dl
l - gtgt pairdist(2 3, 2 3 4)
- ans
- 0 1 2
- 1 0 1
- Â
35??????-2 (IV)
- gtgt A rand(2, 3)
- gtgt B rand(2, 4)
- gtgt C pairdist(A, B)
- C
- 0.5104 0.9806 0.4544 0.5649
- 0.3562 0.2347 0.5911 0.2628
- 0.2101 0.5891 0.2675 0.1639
- Â
- gtgt pairdist('string1', 'string2')
- ??? Input 1 is not a full numerical array!
363-6 ??????????? MEX ????
- ??????? 2 ??,??? MATLAB ????????????
- ??3-4putarray.c
- ????
- gtgt clear all
- gtgt mex putarray.c
- gtgt putarray(1 2 5)
- "putarray_output" is created in the base
workspace. -
- gtgt whos
- Name Size Bytes
Class - putarray_output 1x3 24
double array - Â
- Grand total is 3 elements using 24 bytes
37????
- putarray_output?putarray???? MATLAB?????????,????
- gtgt putarray_output
- putarray_output
- 2 4 10
- ?????????
- ? RS232 ??? GPS ?????,??????????MATLAB
???????,MATLAB ??????????????????????? - ????????????,?? MATLAB ???????,MATLAB
????????????,????????????
383-7 ?????????
- MATLAB ????????????,????????????
- ????C???,??MATLAB??????????????????
- ??3-5matlabStruct2c.c
-
39??????1 (I)
- ????
- ??3-5matlabStruct2c01.m
mex matlabStruct2c.c x(1).name
'???' x(1).phone 2796 x(2).name
'???' x(2).phone 3425 x(3).name
'???' x(3).phone 3213 matlabStruct2c(x)
40??????1 (II)
- ??
- friend0.name ???
- friend0.phone 2796.000000
- friend1.name ???
- friend1.phone 3425.000000
- friend2.name ???
- friend2.phone 3213.000000
- ? MATLAB ??????,?????? C ?????????????
41??????2
- ? C ?????,?? MATLAB ?????????
- ??3-7cStruct2matlab.c
- ????
- ??3-8cStruct2matlab01.m
-
-
- 1x3 struct array with fields
- name
- phone
- MATLAB ?????????? x ??? C ?????????????
mex cStruct2matlab.c x cStruct2matlab
423-8???? C ?????MATLAB ??
- ????? C ???? MATLAB ??
- ???C ???,????? MATLAB ??????????????
- Ex
- ?? MATLAB ?????,???????????????????????(Singlar
Value Decomposition)? - ?? MATLAB ???????,?????????????
43??
- ????? MATLAB ????????????????
- Standalone Executable
- ????MATLAB????? .exe ??
- ?MATLAB 5.x ? 6.x ??Runtime Server????
- ? MATLAB ???????? .exe ??,???????????
- ????? MATLAB ????
- MATLAB 7.x,?Runtime Server????????,??????MATLAB
Compiler????
44C ?? MATLAB ?? (I)
- C ??????? MATLAB ????? M ?? plotSine.m,??????
whos ??????,C ??????? plotViaMatlab01.c? - ? MATLAB ??????? C ???
- ??3-9matlabEngine01.m
-
optsFile matlabroot '\bin\win32\mexopts\msvc60e
ngmatopts.bat' mex('-f', optsFile,
'plotViaMatlab01.c') ???? !plotViaMatlab01
????
45C ?? MATLAB ?? (II)
- ?????,???plotViaMatlab01.exe
- ???????
- ????? MATLAB
- ?? plotSine.m
- ??????
46C ?? MATLAB ?? (III)
- ??????????,??? MATLAB ??? whos ???
- ?????
- ??????????? MATLAB ???,?? MATLAB engine
????????????????????,MATLAB engine ???????
47C ?? MATLAB ??? (I)
- ????? MATLAB Engine???,???????? MATLAB ?? MATLAB
??,????????? - Engine epengOpen(NULL)
48C ?? MATLAB ??? (II)
- ????????MATLAB??plotSine.m ????C???????,? MATLAB
?????? - ????????????
- buffer_getcwd(buffer, BUFSIZE )
- ?? MATLAB ??????
- appapp mxCreateString(buffer)
- ????? app ????????
- appDirengPutVariable(ep, "appDir", app)
- ? MATLAB ?????????? appDir ??????
- engEvalString(ep, "cd(appDir)")
49C ?? MATLAB ??? (III)
- ??????????? MATLAB ?? plotSine.m
????,??????engEvalString(ep, "plotSine")
50C ?? MATLAB ??? (IV)
- ??????? MATLAB ??? whos ??????????
- ?? buffer ???? MATLAB ?????
- engOutputBuffer(ep, buffer, BUFSIZE)
- ? MATLAB engine ?? whos ??
- engEvalString(ep, "whos")
- ?? buffer ???
- MessageBox ((HWND)NULL, (LPSTR)buffer, (LPSTR)
"MATLAB - whos", MB_OK) - ???? MATLAB engine
- engClose(ep)
51????????
- ????????????? MSVC6.0,????????? optsFile
???????????,??????????????? - ??3-11showOptsFile.m
- bcc53engmatopts.bat
df50engmatopts.bat msvc70engmatopts.bat
- bcc54engmatopts.bat df60engmatopts.bat
msvc71engmatopts.bat - bcc55engmatopts.bat
intelc71engmatopts.bat - openwatc13engmatopts.bat
- bcc55freeengmatopts.bat
lccengmatopts.bat - wat11engmatopts.bat
- bcc56engmatopts.bat
msvc50engmatopts.bat - watengmatopts.bat
- bccengmatopts.bat
msvc60engmatopts.bat - ??????????? engmatopts.bat ???,?????????,????????
??????
dir(matlabroot '\bin\win32\mexopts\engmatopts.ba
t')
52C ?? MATLAB ??? (IV)
- ? C ??????? MATLAB ???? MATLAB ??,?????????? M
??? - ??3-12matlabEngine02.m
optsFile matlabroot '\bin\win32\mexopts\msvc60e
ngmatopts.bat' mex('-f', optsFile,
'plotViaMatlab02.c') ???? !plotViaMatlab02
????
53C ?? MATLAB ??? (V)
- ?? plotViaMatlab02.exe ??,???????????????????,
??? - ? plotViaMatlab02.c ????,?????????engClose(ep)???
? MATLAB engine,??????????? MATLAB engine?
54??
- ?????????? C ?? MATLAB ??,??? MATLAB Help??
External Interfaces/API ???Calling MATLAB from C
and Fortran Programs?,????????