Matlab Mex Handouts
Transcript of Matlab Mex Handouts
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 19
Matlab ndash External interfaces
Stefan Johansson Department of Computing Science
Umearing University
Modeling and Simulation Matlab 2
Outline
External interfacesMEX-files
Some about
Java
Component Object Model (COM)
Matlab Compiler
Modeling and Simulation Matlab 3
External interfaces
Possible to call and use external software
and libraries in Matlab
Programsroutines written in
C
C++
FortranJava (built-in support)
Must be compiled into a binary
MEX-file
Modeling and Simulation Matlab 4
Why call external routines
Faster execution
Can use and exchange information withexisting software and libraries
Do not need to rewrite them as m-files
Can use external GUIs eg written inJava or Visual Basic
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 29
Modeling and Simulation Matlab 5
External interfaces
Shared librariesdll (Windows)
so (UNIX and LINUX)
Only Windows
COM (Component Object Model)
Microsoft NET Framework
Can be accessed directlyfrom Matlab(without any need of a
gateway routine)
Modeling and Simulation Matlab 6
MEX-files
External C and Fortran routines compiled forMatlab are called MEX-files
MEX-file extensions
MS Windows
mexw32 mexw64 (from Matlab v 71)
dll (until Matlab v 704)
LINUX mexglx mexa64
Sun Solaris
mexsol mexs64
Modeling and Simulation Matlab 7
MEX-files
C compiler for Windows included (LCC )
Matlab has support for several externalcompilers (eg MS Visual Studio )
External C compiler required forLINUX and UNIX (eg gcc )
External Fortran compiler required (eg
gfortran and Intel Fortran )
Modeling and Simulation Matlab 8
Integrating C files with Matlab
A gateway routine is needed as an interface
between the computational routine(s) andMatlab
The gateway routine must be calledmexFunction (compare with the main function)
Use the mex command to compile thecomputational routine(s) together with thegateway routine into a binary MEX-file
mex can both be called within Matlab and from theWindowsLinux command prompt
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 39
Modeling and Simulation Matlab 9
The MEX-file
Computational and gateway routines canbe in separate files
The first file supplied to the mex commandmust include the gateway routine
The name of the binary MEX-file is the
name of the function called from Matlab
Modeling and Simulation Matlab 10
C MEX-file example
include rdquomexhrdquo
Do the actual computation
double doTheWork(double A)
void mexFunction(int nlhs
mxArray plhs[] int nrhs
const mxArray prhs[])
Call the C function
out = doTheWork(pA)
Call the MEX-file function asa regular Matlab function
gt val = myFunc(A)
myFunccCompile the C MEX-file
(in Matlab or DOSUNIXcommand prompt)
gt mex myFuncc
Must include a gatewayroutine for myFunc
Computational routine
Obligatory header file
Modeling and Simulation Matlab 11
mexFunction() ndash Purpose
Validate the input and output eg check
number type and size of arguments range of values and length of strings
Allocate memory for output Preprocess the data
Type conversion
Call the computational routine(s) The computational routines can be part of the
gateway routine (not recommended)
Postprocess the data
Modeling and Simulation Matlab 12
mexFunction()
mexFunction( int nlhs mxArray plhs[]
983145983150983156 983150983154983144983155983084 983139983151983150983155983156 983149983160983105983154983154983137983161 983082983152983154983144983155983131983133 983081
nlhs ndash The number of left-hand argumentsplhs ndash An array of left-hand output
argumentsnrhs ndash The number of right-hand
argumentsprhs ndash An array of right-hand input
arguments
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 49
Modeling and Simulation Matlab 13
Example 1 ndash meanvecc
m = meanvec(V) computes the mean m of the values in the vector V
include rdquomexhrdquo
define MAX(A B) ((A) gt (B) (A) (B))
define MIN(A B) ((A) lt (B) (A) (B))
Compute the mean of the values in a vector
double vmean(double vec int length)
int i
double sum = 0
for(i = 0 i lt length i++) sum += vec[i]
return sumlength
Modeling and Simulation Matlab 14
meanvecc (cont)void mexFunction( int nlhs mxArray plhs[]
int nrhs const mxArray prhs[])
double Vdouble mean
size_t mn
Check number of arguments
if (nrhs gt 1)
mexErrMsgTxt(Too many input arguments)
else if (nrhs lt 1)
mexErrMsgTxt(Not enough input arguments)
else if (nlhs gt 1)
mexErrMsgTxt(rdquoToo many output argumentsrdquo)
m = mxGetM(prhs[0]) Get number of rows
n = mxGetN(prhs[0]) Get number of columns
Modeling and Simulation Matlab 15
meanvecc (cont) Check dimension and type of input
if (mxIsDouble(prhs[0]) || mxIsComplex(prhs[0]) ||
MIN(mn) = 1)
mexErrMsgTxt(rdquoV must be a non-empty real vectorrdquo)
V = mxGetPr(prhs[0]) Get pointer to start-address
Call computational routine
mean = vmean(VMAX(mn))
Allocate memory for output argument and assign
the mean value to it
plhs[0] = mxCreateDoubleScalar(mean)
return
End of mexFunction
Modeling and Simulation Matlab 16
gtgt mex meanvecc
gtgt m = meanvec([1 2 3 4])
m =
25000
gtgt meanvec([1 2 3 4])
ans =25000
gtgt [mn] = meanvec([1 2 3 4])
Too many output arguments
gtgt m = meanvec([1 2 3 4]4)
Too many input arguments
gtgt m = meanvec([1 2 3 4 5 6 7 8])
Input argument must be a non-empty real vector
gtgt
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 59
Modeling and Simulation Matlab 17
Data types and macros
mxArray (data type)Matlab array
mwSize Array dimensions and number of elements
mwIndex Index values
mwSize and mwIndex are preprocessor macros includedfor cross-platform flexibility
Modeling and Simulation Matlab 18
Some specific mex functions
mxCallocIn a mex-file call mxCalloc to allocate memory
(do not use callocmalloc)
mxFree
Free memory allocated with mxCalloc
mexPrintfas printf
Modeling and Simulation Matlab 19
Error handling
mexWarnMsgTxt issue a Matlab warning
mexErrMsgTxt issue a Matlab error andreturn to Matlab Allocated memory isfreed before termination
Modeling and Simulation Matlab 20
Matlab matrices in C
Matlab stores (complex) matrices column-
vise (C stores arrays row-vise) in twovectors one contains the real data andone contains the imaginary data
Pointer to each vector is obtained withmxGetPr and mxGetPi respectively
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 69
Modeling and Simulation Matlab 21
matrices
mxCreateDoubleMatrixCreates a 2-D matrix of type mxArray
mxCreateSparse
Create a 2-D sparse matrix
mxCreateNumericalArray
Creates an N-D matrix etc
Modeling and Simulation Matlab 22
matrices
mxGetPrGet pointer to first real element in an array
mxGetPi
Get pointer to first complex element in an array
mxGetM mxGetN
Get number of rowscolumns of an array
mxDestroyArray Free memory allocated with mxCreatelowast
Modeling and Simulation Matlab 23
Example 2 ndash lapackLUc
This example will use the routine dgetrf in
the LAPACK library to compute the LU-factorization of a real matrix
For syntax of the computational Fortranroutine in LAPACK see
httpwwwnetliborglapackdoubledgetrff
Modeling and Simulation Matlab 24
lapackLUc (cont)
gtgt mex ndashlargeArrayDims lapackLUc -lmwlapack
To compile the source code the LAPACK
library need to be included
The LAPACK library isincluded with Matlab
Support of 64-bit API(64-bit integers)
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 79
Modeling and Simulation Matlab 25
lapackLU testgtgt A = rand(5)
gtgt [LUP] = lapackLU(A)
L =
10000 0 0 0 0
03098 10000 0 0 0
05177 05538 10000 0 0
01236 00475 00414 10000 0
04995 09573 09230 -06548 10000
U =
07803 01320 02348 01690 05470
0 09153 07485 06794 05753
0 0 -05207 01840 -04128
0 0 0 03902 06089
0 0 0 0 02522
P =
1 0 0 0 0
0 0 0 0 1
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
gtgt norm(A-PLU)
ans = 12413e-16
Modeling and Simulation Matlab 26
Mex configuration
The option file for mex is mexoptssh allocated in (search order)Current directory
Matlabroot bin
The local preferences directory (returned byprefdir ) On Linux usually in
rsquoYour home directory rsquomatlab Matlabversion To configure mex (eg to select compiler)
type rsquomex -setup rsquo in Matlab
Modeling and Simulation Matlab 27
Mex options
-v Verbose
-Iltpath gt Search for header files inltpath gt
-lltlibrary gt Link with library ltlibrary gt
-Lltpath gt Search for libraries in ltpath gt
-Dltname gt Define the symbol ltname gt
-output ltname gt Write output toltnamegtmexlowast
Modeling and Simulation Matlab 28
Makefile example MEX = mex
MEXOPT = -O -largeArrayDims
MEXLIBS = -lmwlapack
MEXDEBUG = -g -DDEBUG
RM = rm -f
Rules for targets
default all
all lapackLU clean
lapackLU
echo Compile MEX-file
$(MEX) $(MEXOPT) -output $ lapackLUc $(MEXLIBS)
echo Done
debug
echo Compile MEX-file for debugging$(MEX) $(MEXDEBUG) -output lapackLU lapackLUc $(MEXLIBS)
echo Done
clean
$(RM) o
Makefile for lapackLUc
Targets
lapackLU Create the mex file lapackLUmex
debug Compile mex-file with debug
information and trace outputs clean Remove all object files
Needed before compiling for a new
architecture
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 89
Modeling and Simulation Matlab 29
For more information
Go to Matlab helpMatlab External Interfaces
Or visithttpwwwmathworksseaccesshelpdeskhelptechdocmatlab_externalbp_kqh7html
Modeling and Simulation Matlab 30
Calling Java from Matlab
Intergrated in Matlab (since Matlab 6) Can call native (built-in) as well as external
Java classes directly from the Matlabcommand prompt
The Java classes do not need to be
compiled explicitly for Matlab No gateway function needed in the Java
class
Modeling and Simulation Matlab 31
The other way around
Possible to call Matlab functions from an
external programUNIX Through pipes
Windows through a COM interface
Use Matlab as computation engine
Modeling and Simulation Matlab 32
Component Object Model
Component Object Model (COM) ndash
Framework for integrating existingsoftware into an application
Software can be written in any language thatsupports COM
The applications communicate through aCOM object
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 99
Modeling and Simulation Matlab 33
Calling Matlab from Java
Is officially not supported by Mathworks
However
jmijar included to support this functionality
Third-party Java-Matlab interfaces exist
Modeling and Simulation Matlab 34
Compiled Matlab code
Compile your Matlab code to stand-aloneapplications or software
Matlab is not needed by the end-user
Requirements
Matlab Compiler
An ANSI C or C++ compiler
Restrictions Do not work with all toolboxes (or parts of)
Most pre-built GUIs can not be included
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 29
Modeling and Simulation Matlab 5
External interfaces
Shared librariesdll (Windows)
so (UNIX and LINUX)
Only Windows
COM (Component Object Model)
Microsoft NET Framework
Can be accessed directlyfrom Matlab(without any need of a
gateway routine)
Modeling and Simulation Matlab 6
MEX-files
External C and Fortran routines compiled forMatlab are called MEX-files
MEX-file extensions
MS Windows
mexw32 mexw64 (from Matlab v 71)
dll (until Matlab v 704)
LINUX mexglx mexa64
Sun Solaris
mexsol mexs64
Modeling and Simulation Matlab 7
MEX-files
C compiler for Windows included (LCC )
Matlab has support for several externalcompilers (eg MS Visual Studio )
External C compiler required forLINUX and UNIX (eg gcc )
External Fortran compiler required (eg
gfortran and Intel Fortran )
Modeling and Simulation Matlab 8
Integrating C files with Matlab
A gateway routine is needed as an interface
between the computational routine(s) andMatlab
The gateway routine must be calledmexFunction (compare with the main function)
Use the mex command to compile thecomputational routine(s) together with thegateway routine into a binary MEX-file
mex can both be called within Matlab and from theWindowsLinux command prompt
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 39
Modeling and Simulation Matlab 9
The MEX-file
Computational and gateway routines canbe in separate files
The first file supplied to the mex commandmust include the gateway routine
The name of the binary MEX-file is the
name of the function called from Matlab
Modeling and Simulation Matlab 10
C MEX-file example
include rdquomexhrdquo
Do the actual computation
double doTheWork(double A)
void mexFunction(int nlhs
mxArray plhs[] int nrhs
const mxArray prhs[])
Call the C function
out = doTheWork(pA)
Call the MEX-file function asa regular Matlab function
gt val = myFunc(A)
myFunccCompile the C MEX-file
(in Matlab or DOSUNIXcommand prompt)
gt mex myFuncc
Must include a gatewayroutine for myFunc
Computational routine
Obligatory header file
Modeling and Simulation Matlab 11
mexFunction() ndash Purpose
Validate the input and output eg check
number type and size of arguments range of values and length of strings
Allocate memory for output Preprocess the data
Type conversion
Call the computational routine(s) The computational routines can be part of the
gateway routine (not recommended)
Postprocess the data
Modeling and Simulation Matlab 12
mexFunction()
mexFunction( int nlhs mxArray plhs[]
983145983150983156 983150983154983144983155983084 983139983151983150983155983156 983149983160983105983154983154983137983161 983082983152983154983144983155983131983133 983081
nlhs ndash The number of left-hand argumentsplhs ndash An array of left-hand output
argumentsnrhs ndash The number of right-hand
argumentsprhs ndash An array of right-hand input
arguments
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 49
Modeling and Simulation Matlab 13
Example 1 ndash meanvecc
m = meanvec(V) computes the mean m of the values in the vector V
include rdquomexhrdquo
define MAX(A B) ((A) gt (B) (A) (B))
define MIN(A B) ((A) lt (B) (A) (B))
Compute the mean of the values in a vector
double vmean(double vec int length)
int i
double sum = 0
for(i = 0 i lt length i++) sum += vec[i]
return sumlength
Modeling and Simulation Matlab 14
meanvecc (cont)void mexFunction( int nlhs mxArray plhs[]
int nrhs const mxArray prhs[])
double Vdouble mean
size_t mn
Check number of arguments
if (nrhs gt 1)
mexErrMsgTxt(Too many input arguments)
else if (nrhs lt 1)
mexErrMsgTxt(Not enough input arguments)
else if (nlhs gt 1)
mexErrMsgTxt(rdquoToo many output argumentsrdquo)
m = mxGetM(prhs[0]) Get number of rows
n = mxGetN(prhs[0]) Get number of columns
Modeling and Simulation Matlab 15
meanvecc (cont) Check dimension and type of input
if (mxIsDouble(prhs[0]) || mxIsComplex(prhs[0]) ||
MIN(mn) = 1)
mexErrMsgTxt(rdquoV must be a non-empty real vectorrdquo)
V = mxGetPr(prhs[0]) Get pointer to start-address
Call computational routine
mean = vmean(VMAX(mn))
Allocate memory for output argument and assign
the mean value to it
plhs[0] = mxCreateDoubleScalar(mean)
return
End of mexFunction
Modeling and Simulation Matlab 16
gtgt mex meanvecc
gtgt m = meanvec([1 2 3 4])
m =
25000
gtgt meanvec([1 2 3 4])
ans =25000
gtgt [mn] = meanvec([1 2 3 4])
Too many output arguments
gtgt m = meanvec([1 2 3 4]4)
Too many input arguments
gtgt m = meanvec([1 2 3 4 5 6 7 8])
Input argument must be a non-empty real vector
gtgt
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 59
Modeling and Simulation Matlab 17
Data types and macros
mxArray (data type)Matlab array
mwSize Array dimensions and number of elements
mwIndex Index values
mwSize and mwIndex are preprocessor macros includedfor cross-platform flexibility
Modeling and Simulation Matlab 18
Some specific mex functions
mxCallocIn a mex-file call mxCalloc to allocate memory
(do not use callocmalloc)
mxFree
Free memory allocated with mxCalloc
mexPrintfas printf
Modeling and Simulation Matlab 19
Error handling
mexWarnMsgTxt issue a Matlab warning
mexErrMsgTxt issue a Matlab error andreturn to Matlab Allocated memory isfreed before termination
Modeling and Simulation Matlab 20
Matlab matrices in C
Matlab stores (complex) matrices column-
vise (C stores arrays row-vise) in twovectors one contains the real data andone contains the imaginary data
Pointer to each vector is obtained withmxGetPr and mxGetPi respectively
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 69
Modeling and Simulation Matlab 21
matrices
mxCreateDoubleMatrixCreates a 2-D matrix of type mxArray
mxCreateSparse
Create a 2-D sparse matrix
mxCreateNumericalArray
Creates an N-D matrix etc
Modeling and Simulation Matlab 22
matrices
mxGetPrGet pointer to first real element in an array
mxGetPi
Get pointer to first complex element in an array
mxGetM mxGetN
Get number of rowscolumns of an array
mxDestroyArray Free memory allocated with mxCreatelowast
Modeling and Simulation Matlab 23
Example 2 ndash lapackLUc
This example will use the routine dgetrf in
the LAPACK library to compute the LU-factorization of a real matrix
For syntax of the computational Fortranroutine in LAPACK see
httpwwwnetliborglapackdoubledgetrff
Modeling and Simulation Matlab 24
lapackLUc (cont)
gtgt mex ndashlargeArrayDims lapackLUc -lmwlapack
To compile the source code the LAPACK
library need to be included
The LAPACK library isincluded with Matlab
Support of 64-bit API(64-bit integers)
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 79
Modeling and Simulation Matlab 25
lapackLU testgtgt A = rand(5)
gtgt [LUP] = lapackLU(A)
L =
10000 0 0 0 0
03098 10000 0 0 0
05177 05538 10000 0 0
01236 00475 00414 10000 0
04995 09573 09230 -06548 10000
U =
07803 01320 02348 01690 05470
0 09153 07485 06794 05753
0 0 -05207 01840 -04128
0 0 0 03902 06089
0 0 0 0 02522
P =
1 0 0 0 0
0 0 0 0 1
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
gtgt norm(A-PLU)
ans = 12413e-16
Modeling and Simulation Matlab 26
Mex configuration
The option file for mex is mexoptssh allocated in (search order)Current directory
Matlabroot bin
The local preferences directory (returned byprefdir ) On Linux usually in
rsquoYour home directory rsquomatlab Matlabversion To configure mex (eg to select compiler)
type rsquomex -setup rsquo in Matlab
Modeling and Simulation Matlab 27
Mex options
-v Verbose
-Iltpath gt Search for header files inltpath gt
-lltlibrary gt Link with library ltlibrary gt
-Lltpath gt Search for libraries in ltpath gt
-Dltname gt Define the symbol ltname gt
-output ltname gt Write output toltnamegtmexlowast
Modeling and Simulation Matlab 28
Makefile example MEX = mex
MEXOPT = -O -largeArrayDims
MEXLIBS = -lmwlapack
MEXDEBUG = -g -DDEBUG
RM = rm -f
Rules for targets
default all
all lapackLU clean
lapackLU
echo Compile MEX-file
$(MEX) $(MEXOPT) -output $ lapackLUc $(MEXLIBS)
echo Done
debug
echo Compile MEX-file for debugging$(MEX) $(MEXDEBUG) -output lapackLU lapackLUc $(MEXLIBS)
echo Done
clean
$(RM) o
Makefile for lapackLUc
Targets
lapackLU Create the mex file lapackLUmex
debug Compile mex-file with debug
information and trace outputs clean Remove all object files
Needed before compiling for a new
architecture
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 89
Modeling and Simulation Matlab 29
For more information
Go to Matlab helpMatlab External Interfaces
Or visithttpwwwmathworksseaccesshelpdeskhelptechdocmatlab_externalbp_kqh7html
Modeling and Simulation Matlab 30
Calling Java from Matlab
Intergrated in Matlab (since Matlab 6) Can call native (built-in) as well as external
Java classes directly from the Matlabcommand prompt
The Java classes do not need to be
compiled explicitly for Matlab No gateway function needed in the Java
class
Modeling and Simulation Matlab 31
The other way around
Possible to call Matlab functions from an
external programUNIX Through pipes
Windows through a COM interface
Use Matlab as computation engine
Modeling and Simulation Matlab 32
Component Object Model
Component Object Model (COM) ndash
Framework for integrating existingsoftware into an application
Software can be written in any language thatsupports COM
The applications communicate through aCOM object
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 99
Modeling and Simulation Matlab 33
Calling Matlab from Java
Is officially not supported by Mathworks
However
jmijar included to support this functionality
Third-party Java-Matlab interfaces exist
Modeling and Simulation Matlab 34
Compiled Matlab code
Compile your Matlab code to stand-aloneapplications or software
Matlab is not needed by the end-user
Requirements
Matlab Compiler
An ANSI C or C++ compiler
Restrictions Do not work with all toolboxes (or parts of)
Most pre-built GUIs can not be included
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 39
Modeling and Simulation Matlab 9
The MEX-file
Computational and gateway routines canbe in separate files
The first file supplied to the mex commandmust include the gateway routine
The name of the binary MEX-file is the
name of the function called from Matlab
Modeling and Simulation Matlab 10
C MEX-file example
include rdquomexhrdquo
Do the actual computation
double doTheWork(double A)
void mexFunction(int nlhs
mxArray plhs[] int nrhs
const mxArray prhs[])
Call the C function
out = doTheWork(pA)
Call the MEX-file function asa regular Matlab function
gt val = myFunc(A)
myFunccCompile the C MEX-file
(in Matlab or DOSUNIXcommand prompt)
gt mex myFuncc
Must include a gatewayroutine for myFunc
Computational routine
Obligatory header file
Modeling and Simulation Matlab 11
mexFunction() ndash Purpose
Validate the input and output eg check
number type and size of arguments range of values and length of strings
Allocate memory for output Preprocess the data
Type conversion
Call the computational routine(s) The computational routines can be part of the
gateway routine (not recommended)
Postprocess the data
Modeling and Simulation Matlab 12
mexFunction()
mexFunction( int nlhs mxArray plhs[]
983145983150983156 983150983154983144983155983084 983139983151983150983155983156 983149983160983105983154983154983137983161 983082983152983154983144983155983131983133 983081
nlhs ndash The number of left-hand argumentsplhs ndash An array of left-hand output
argumentsnrhs ndash The number of right-hand
argumentsprhs ndash An array of right-hand input
arguments
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 49
Modeling and Simulation Matlab 13
Example 1 ndash meanvecc
m = meanvec(V) computes the mean m of the values in the vector V
include rdquomexhrdquo
define MAX(A B) ((A) gt (B) (A) (B))
define MIN(A B) ((A) lt (B) (A) (B))
Compute the mean of the values in a vector
double vmean(double vec int length)
int i
double sum = 0
for(i = 0 i lt length i++) sum += vec[i]
return sumlength
Modeling and Simulation Matlab 14
meanvecc (cont)void mexFunction( int nlhs mxArray plhs[]
int nrhs const mxArray prhs[])
double Vdouble mean
size_t mn
Check number of arguments
if (nrhs gt 1)
mexErrMsgTxt(Too many input arguments)
else if (nrhs lt 1)
mexErrMsgTxt(Not enough input arguments)
else if (nlhs gt 1)
mexErrMsgTxt(rdquoToo many output argumentsrdquo)
m = mxGetM(prhs[0]) Get number of rows
n = mxGetN(prhs[0]) Get number of columns
Modeling and Simulation Matlab 15
meanvecc (cont) Check dimension and type of input
if (mxIsDouble(prhs[0]) || mxIsComplex(prhs[0]) ||
MIN(mn) = 1)
mexErrMsgTxt(rdquoV must be a non-empty real vectorrdquo)
V = mxGetPr(prhs[0]) Get pointer to start-address
Call computational routine
mean = vmean(VMAX(mn))
Allocate memory for output argument and assign
the mean value to it
plhs[0] = mxCreateDoubleScalar(mean)
return
End of mexFunction
Modeling and Simulation Matlab 16
gtgt mex meanvecc
gtgt m = meanvec([1 2 3 4])
m =
25000
gtgt meanvec([1 2 3 4])
ans =25000
gtgt [mn] = meanvec([1 2 3 4])
Too many output arguments
gtgt m = meanvec([1 2 3 4]4)
Too many input arguments
gtgt m = meanvec([1 2 3 4 5 6 7 8])
Input argument must be a non-empty real vector
gtgt
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 59
Modeling and Simulation Matlab 17
Data types and macros
mxArray (data type)Matlab array
mwSize Array dimensions and number of elements
mwIndex Index values
mwSize and mwIndex are preprocessor macros includedfor cross-platform flexibility
Modeling and Simulation Matlab 18
Some specific mex functions
mxCallocIn a mex-file call mxCalloc to allocate memory
(do not use callocmalloc)
mxFree
Free memory allocated with mxCalloc
mexPrintfas printf
Modeling and Simulation Matlab 19
Error handling
mexWarnMsgTxt issue a Matlab warning
mexErrMsgTxt issue a Matlab error andreturn to Matlab Allocated memory isfreed before termination
Modeling and Simulation Matlab 20
Matlab matrices in C
Matlab stores (complex) matrices column-
vise (C stores arrays row-vise) in twovectors one contains the real data andone contains the imaginary data
Pointer to each vector is obtained withmxGetPr and mxGetPi respectively
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 69
Modeling and Simulation Matlab 21
matrices
mxCreateDoubleMatrixCreates a 2-D matrix of type mxArray
mxCreateSparse
Create a 2-D sparse matrix
mxCreateNumericalArray
Creates an N-D matrix etc
Modeling and Simulation Matlab 22
matrices
mxGetPrGet pointer to first real element in an array
mxGetPi
Get pointer to first complex element in an array
mxGetM mxGetN
Get number of rowscolumns of an array
mxDestroyArray Free memory allocated with mxCreatelowast
Modeling and Simulation Matlab 23
Example 2 ndash lapackLUc
This example will use the routine dgetrf in
the LAPACK library to compute the LU-factorization of a real matrix
For syntax of the computational Fortranroutine in LAPACK see
httpwwwnetliborglapackdoubledgetrff
Modeling and Simulation Matlab 24
lapackLUc (cont)
gtgt mex ndashlargeArrayDims lapackLUc -lmwlapack
To compile the source code the LAPACK
library need to be included
The LAPACK library isincluded with Matlab
Support of 64-bit API(64-bit integers)
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 79
Modeling and Simulation Matlab 25
lapackLU testgtgt A = rand(5)
gtgt [LUP] = lapackLU(A)
L =
10000 0 0 0 0
03098 10000 0 0 0
05177 05538 10000 0 0
01236 00475 00414 10000 0
04995 09573 09230 -06548 10000
U =
07803 01320 02348 01690 05470
0 09153 07485 06794 05753
0 0 -05207 01840 -04128
0 0 0 03902 06089
0 0 0 0 02522
P =
1 0 0 0 0
0 0 0 0 1
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
gtgt norm(A-PLU)
ans = 12413e-16
Modeling and Simulation Matlab 26
Mex configuration
The option file for mex is mexoptssh allocated in (search order)Current directory
Matlabroot bin
The local preferences directory (returned byprefdir ) On Linux usually in
rsquoYour home directory rsquomatlab Matlabversion To configure mex (eg to select compiler)
type rsquomex -setup rsquo in Matlab
Modeling and Simulation Matlab 27
Mex options
-v Verbose
-Iltpath gt Search for header files inltpath gt
-lltlibrary gt Link with library ltlibrary gt
-Lltpath gt Search for libraries in ltpath gt
-Dltname gt Define the symbol ltname gt
-output ltname gt Write output toltnamegtmexlowast
Modeling and Simulation Matlab 28
Makefile example MEX = mex
MEXOPT = -O -largeArrayDims
MEXLIBS = -lmwlapack
MEXDEBUG = -g -DDEBUG
RM = rm -f
Rules for targets
default all
all lapackLU clean
lapackLU
echo Compile MEX-file
$(MEX) $(MEXOPT) -output $ lapackLUc $(MEXLIBS)
echo Done
debug
echo Compile MEX-file for debugging$(MEX) $(MEXDEBUG) -output lapackLU lapackLUc $(MEXLIBS)
echo Done
clean
$(RM) o
Makefile for lapackLUc
Targets
lapackLU Create the mex file lapackLUmex
debug Compile mex-file with debug
information and trace outputs clean Remove all object files
Needed before compiling for a new
architecture
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 89
Modeling and Simulation Matlab 29
For more information
Go to Matlab helpMatlab External Interfaces
Or visithttpwwwmathworksseaccesshelpdeskhelptechdocmatlab_externalbp_kqh7html
Modeling and Simulation Matlab 30
Calling Java from Matlab
Intergrated in Matlab (since Matlab 6) Can call native (built-in) as well as external
Java classes directly from the Matlabcommand prompt
The Java classes do not need to be
compiled explicitly for Matlab No gateway function needed in the Java
class
Modeling and Simulation Matlab 31
The other way around
Possible to call Matlab functions from an
external programUNIX Through pipes
Windows through a COM interface
Use Matlab as computation engine
Modeling and Simulation Matlab 32
Component Object Model
Component Object Model (COM) ndash
Framework for integrating existingsoftware into an application
Software can be written in any language thatsupports COM
The applications communicate through aCOM object
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 99
Modeling and Simulation Matlab 33
Calling Matlab from Java
Is officially not supported by Mathworks
However
jmijar included to support this functionality
Third-party Java-Matlab interfaces exist
Modeling and Simulation Matlab 34
Compiled Matlab code
Compile your Matlab code to stand-aloneapplications or software
Matlab is not needed by the end-user
Requirements
Matlab Compiler
An ANSI C or C++ compiler
Restrictions Do not work with all toolboxes (or parts of)
Most pre-built GUIs can not be included
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 49
Modeling and Simulation Matlab 13
Example 1 ndash meanvecc
m = meanvec(V) computes the mean m of the values in the vector V
include rdquomexhrdquo
define MAX(A B) ((A) gt (B) (A) (B))
define MIN(A B) ((A) lt (B) (A) (B))
Compute the mean of the values in a vector
double vmean(double vec int length)
int i
double sum = 0
for(i = 0 i lt length i++) sum += vec[i]
return sumlength
Modeling and Simulation Matlab 14
meanvecc (cont)void mexFunction( int nlhs mxArray plhs[]
int nrhs const mxArray prhs[])
double Vdouble mean
size_t mn
Check number of arguments
if (nrhs gt 1)
mexErrMsgTxt(Too many input arguments)
else if (nrhs lt 1)
mexErrMsgTxt(Not enough input arguments)
else if (nlhs gt 1)
mexErrMsgTxt(rdquoToo many output argumentsrdquo)
m = mxGetM(prhs[0]) Get number of rows
n = mxGetN(prhs[0]) Get number of columns
Modeling and Simulation Matlab 15
meanvecc (cont) Check dimension and type of input
if (mxIsDouble(prhs[0]) || mxIsComplex(prhs[0]) ||
MIN(mn) = 1)
mexErrMsgTxt(rdquoV must be a non-empty real vectorrdquo)
V = mxGetPr(prhs[0]) Get pointer to start-address
Call computational routine
mean = vmean(VMAX(mn))
Allocate memory for output argument and assign
the mean value to it
plhs[0] = mxCreateDoubleScalar(mean)
return
End of mexFunction
Modeling and Simulation Matlab 16
gtgt mex meanvecc
gtgt m = meanvec([1 2 3 4])
m =
25000
gtgt meanvec([1 2 3 4])
ans =25000
gtgt [mn] = meanvec([1 2 3 4])
Too many output arguments
gtgt m = meanvec([1 2 3 4]4)
Too many input arguments
gtgt m = meanvec([1 2 3 4 5 6 7 8])
Input argument must be a non-empty real vector
gtgt
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 59
Modeling and Simulation Matlab 17
Data types and macros
mxArray (data type)Matlab array
mwSize Array dimensions and number of elements
mwIndex Index values
mwSize and mwIndex are preprocessor macros includedfor cross-platform flexibility
Modeling and Simulation Matlab 18
Some specific mex functions
mxCallocIn a mex-file call mxCalloc to allocate memory
(do not use callocmalloc)
mxFree
Free memory allocated with mxCalloc
mexPrintfas printf
Modeling and Simulation Matlab 19
Error handling
mexWarnMsgTxt issue a Matlab warning
mexErrMsgTxt issue a Matlab error andreturn to Matlab Allocated memory isfreed before termination
Modeling and Simulation Matlab 20
Matlab matrices in C
Matlab stores (complex) matrices column-
vise (C stores arrays row-vise) in twovectors one contains the real data andone contains the imaginary data
Pointer to each vector is obtained withmxGetPr and mxGetPi respectively
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 69
Modeling and Simulation Matlab 21
matrices
mxCreateDoubleMatrixCreates a 2-D matrix of type mxArray
mxCreateSparse
Create a 2-D sparse matrix
mxCreateNumericalArray
Creates an N-D matrix etc
Modeling and Simulation Matlab 22
matrices
mxGetPrGet pointer to first real element in an array
mxGetPi
Get pointer to first complex element in an array
mxGetM mxGetN
Get number of rowscolumns of an array
mxDestroyArray Free memory allocated with mxCreatelowast
Modeling and Simulation Matlab 23
Example 2 ndash lapackLUc
This example will use the routine dgetrf in
the LAPACK library to compute the LU-factorization of a real matrix
For syntax of the computational Fortranroutine in LAPACK see
httpwwwnetliborglapackdoubledgetrff
Modeling and Simulation Matlab 24
lapackLUc (cont)
gtgt mex ndashlargeArrayDims lapackLUc -lmwlapack
To compile the source code the LAPACK
library need to be included
The LAPACK library isincluded with Matlab
Support of 64-bit API(64-bit integers)
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 79
Modeling and Simulation Matlab 25
lapackLU testgtgt A = rand(5)
gtgt [LUP] = lapackLU(A)
L =
10000 0 0 0 0
03098 10000 0 0 0
05177 05538 10000 0 0
01236 00475 00414 10000 0
04995 09573 09230 -06548 10000
U =
07803 01320 02348 01690 05470
0 09153 07485 06794 05753
0 0 -05207 01840 -04128
0 0 0 03902 06089
0 0 0 0 02522
P =
1 0 0 0 0
0 0 0 0 1
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
gtgt norm(A-PLU)
ans = 12413e-16
Modeling and Simulation Matlab 26
Mex configuration
The option file for mex is mexoptssh allocated in (search order)Current directory
Matlabroot bin
The local preferences directory (returned byprefdir ) On Linux usually in
rsquoYour home directory rsquomatlab Matlabversion To configure mex (eg to select compiler)
type rsquomex -setup rsquo in Matlab
Modeling and Simulation Matlab 27
Mex options
-v Verbose
-Iltpath gt Search for header files inltpath gt
-lltlibrary gt Link with library ltlibrary gt
-Lltpath gt Search for libraries in ltpath gt
-Dltname gt Define the symbol ltname gt
-output ltname gt Write output toltnamegtmexlowast
Modeling and Simulation Matlab 28
Makefile example MEX = mex
MEXOPT = -O -largeArrayDims
MEXLIBS = -lmwlapack
MEXDEBUG = -g -DDEBUG
RM = rm -f
Rules for targets
default all
all lapackLU clean
lapackLU
echo Compile MEX-file
$(MEX) $(MEXOPT) -output $ lapackLUc $(MEXLIBS)
echo Done
debug
echo Compile MEX-file for debugging$(MEX) $(MEXDEBUG) -output lapackLU lapackLUc $(MEXLIBS)
echo Done
clean
$(RM) o
Makefile for lapackLUc
Targets
lapackLU Create the mex file lapackLUmex
debug Compile mex-file with debug
information and trace outputs clean Remove all object files
Needed before compiling for a new
architecture
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 89
Modeling and Simulation Matlab 29
For more information
Go to Matlab helpMatlab External Interfaces
Or visithttpwwwmathworksseaccesshelpdeskhelptechdocmatlab_externalbp_kqh7html
Modeling and Simulation Matlab 30
Calling Java from Matlab
Intergrated in Matlab (since Matlab 6) Can call native (built-in) as well as external
Java classes directly from the Matlabcommand prompt
The Java classes do not need to be
compiled explicitly for Matlab No gateway function needed in the Java
class
Modeling and Simulation Matlab 31
The other way around
Possible to call Matlab functions from an
external programUNIX Through pipes
Windows through a COM interface
Use Matlab as computation engine
Modeling and Simulation Matlab 32
Component Object Model
Component Object Model (COM) ndash
Framework for integrating existingsoftware into an application
Software can be written in any language thatsupports COM
The applications communicate through aCOM object
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 99
Modeling and Simulation Matlab 33
Calling Matlab from Java
Is officially not supported by Mathworks
However
jmijar included to support this functionality
Third-party Java-Matlab interfaces exist
Modeling and Simulation Matlab 34
Compiled Matlab code
Compile your Matlab code to stand-aloneapplications or software
Matlab is not needed by the end-user
Requirements
Matlab Compiler
An ANSI C or C++ compiler
Restrictions Do not work with all toolboxes (or parts of)
Most pre-built GUIs can not be included
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 59
Modeling and Simulation Matlab 17
Data types and macros
mxArray (data type)Matlab array
mwSize Array dimensions and number of elements
mwIndex Index values
mwSize and mwIndex are preprocessor macros includedfor cross-platform flexibility
Modeling and Simulation Matlab 18
Some specific mex functions
mxCallocIn a mex-file call mxCalloc to allocate memory
(do not use callocmalloc)
mxFree
Free memory allocated with mxCalloc
mexPrintfas printf
Modeling and Simulation Matlab 19
Error handling
mexWarnMsgTxt issue a Matlab warning
mexErrMsgTxt issue a Matlab error andreturn to Matlab Allocated memory isfreed before termination
Modeling and Simulation Matlab 20
Matlab matrices in C
Matlab stores (complex) matrices column-
vise (C stores arrays row-vise) in twovectors one contains the real data andone contains the imaginary data
Pointer to each vector is obtained withmxGetPr and mxGetPi respectively
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 69
Modeling and Simulation Matlab 21
matrices
mxCreateDoubleMatrixCreates a 2-D matrix of type mxArray
mxCreateSparse
Create a 2-D sparse matrix
mxCreateNumericalArray
Creates an N-D matrix etc
Modeling and Simulation Matlab 22
matrices
mxGetPrGet pointer to first real element in an array
mxGetPi
Get pointer to first complex element in an array
mxGetM mxGetN
Get number of rowscolumns of an array
mxDestroyArray Free memory allocated with mxCreatelowast
Modeling and Simulation Matlab 23
Example 2 ndash lapackLUc
This example will use the routine dgetrf in
the LAPACK library to compute the LU-factorization of a real matrix
For syntax of the computational Fortranroutine in LAPACK see
httpwwwnetliborglapackdoubledgetrff
Modeling and Simulation Matlab 24
lapackLUc (cont)
gtgt mex ndashlargeArrayDims lapackLUc -lmwlapack
To compile the source code the LAPACK
library need to be included
The LAPACK library isincluded with Matlab
Support of 64-bit API(64-bit integers)
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 79
Modeling and Simulation Matlab 25
lapackLU testgtgt A = rand(5)
gtgt [LUP] = lapackLU(A)
L =
10000 0 0 0 0
03098 10000 0 0 0
05177 05538 10000 0 0
01236 00475 00414 10000 0
04995 09573 09230 -06548 10000
U =
07803 01320 02348 01690 05470
0 09153 07485 06794 05753
0 0 -05207 01840 -04128
0 0 0 03902 06089
0 0 0 0 02522
P =
1 0 0 0 0
0 0 0 0 1
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
gtgt norm(A-PLU)
ans = 12413e-16
Modeling and Simulation Matlab 26
Mex configuration
The option file for mex is mexoptssh allocated in (search order)Current directory
Matlabroot bin
The local preferences directory (returned byprefdir ) On Linux usually in
rsquoYour home directory rsquomatlab Matlabversion To configure mex (eg to select compiler)
type rsquomex -setup rsquo in Matlab
Modeling and Simulation Matlab 27
Mex options
-v Verbose
-Iltpath gt Search for header files inltpath gt
-lltlibrary gt Link with library ltlibrary gt
-Lltpath gt Search for libraries in ltpath gt
-Dltname gt Define the symbol ltname gt
-output ltname gt Write output toltnamegtmexlowast
Modeling and Simulation Matlab 28
Makefile example MEX = mex
MEXOPT = -O -largeArrayDims
MEXLIBS = -lmwlapack
MEXDEBUG = -g -DDEBUG
RM = rm -f
Rules for targets
default all
all lapackLU clean
lapackLU
echo Compile MEX-file
$(MEX) $(MEXOPT) -output $ lapackLUc $(MEXLIBS)
echo Done
debug
echo Compile MEX-file for debugging$(MEX) $(MEXDEBUG) -output lapackLU lapackLUc $(MEXLIBS)
echo Done
clean
$(RM) o
Makefile for lapackLUc
Targets
lapackLU Create the mex file lapackLUmex
debug Compile mex-file with debug
information and trace outputs clean Remove all object files
Needed before compiling for a new
architecture
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 89
Modeling and Simulation Matlab 29
For more information
Go to Matlab helpMatlab External Interfaces
Or visithttpwwwmathworksseaccesshelpdeskhelptechdocmatlab_externalbp_kqh7html
Modeling and Simulation Matlab 30
Calling Java from Matlab
Intergrated in Matlab (since Matlab 6) Can call native (built-in) as well as external
Java classes directly from the Matlabcommand prompt
The Java classes do not need to be
compiled explicitly for Matlab No gateway function needed in the Java
class
Modeling and Simulation Matlab 31
The other way around
Possible to call Matlab functions from an
external programUNIX Through pipes
Windows through a COM interface
Use Matlab as computation engine
Modeling and Simulation Matlab 32
Component Object Model
Component Object Model (COM) ndash
Framework for integrating existingsoftware into an application
Software can be written in any language thatsupports COM
The applications communicate through aCOM object
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 99
Modeling and Simulation Matlab 33
Calling Matlab from Java
Is officially not supported by Mathworks
However
jmijar included to support this functionality
Third-party Java-Matlab interfaces exist
Modeling and Simulation Matlab 34
Compiled Matlab code
Compile your Matlab code to stand-aloneapplications or software
Matlab is not needed by the end-user
Requirements
Matlab Compiler
An ANSI C or C++ compiler
Restrictions Do not work with all toolboxes (or parts of)
Most pre-built GUIs can not be included
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 69
Modeling and Simulation Matlab 21
matrices
mxCreateDoubleMatrixCreates a 2-D matrix of type mxArray
mxCreateSparse
Create a 2-D sparse matrix
mxCreateNumericalArray
Creates an N-D matrix etc
Modeling and Simulation Matlab 22
matrices
mxGetPrGet pointer to first real element in an array
mxGetPi
Get pointer to first complex element in an array
mxGetM mxGetN
Get number of rowscolumns of an array
mxDestroyArray Free memory allocated with mxCreatelowast
Modeling and Simulation Matlab 23
Example 2 ndash lapackLUc
This example will use the routine dgetrf in
the LAPACK library to compute the LU-factorization of a real matrix
For syntax of the computational Fortranroutine in LAPACK see
httpwwwnetliborglapackdoubledgetrff
Modeling and Simulation Matlab 24
lapackLUc (cont)
gtgt mex ndashlargeArrayDims lapackLUc -lmwlapack
To compile the source code the LAPACK
library need to be included
The LAPACK library isincluded with Matlab
Support of 64-bit API(64-bit integers)
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 79
Modeling and Simulation Matlab 25
lapackLU testgtgt A = rand(5)
gtgt [LUP] = lapackLU(A)
L =
10000 0 0 0 0
03098 10000 0 0 0
05177 05538 10000 0 0
01236 00475 00414 10000 0
04995 09573 09230 -06548 10000
U =
07803 01320 02348 01690 05470
0 09153 07485 06794 05753
0 0 -05207 01840 -04128
0 0 0 03902 06089
0 0 0 0 02522
P =
1 0 0 0 0
0 0 0 0 1
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
gtgt norm(A-PLU)
ans = 12413e-16
Modeling and Simulation Matlab 26
Mex configuration
The option file for mex is mexoptssh allocated in (search order)Current directory
Matlabroot bin
The local preferences directory (returned byprefdir ) On Linux usually in
rsquoYour home directory rsquomatlab Matlabversion To configure mex (eg to select compiler)
type rsquomex -setup rsquo in Matlab
Modeling and Simulation Matlab 27
Mex options
-v Verbose
-Iltpath gt Search for header files inltpath gt
-lltlibrary gt Link with library ltlibrary gt
-Lltpath gt Search for libraries in ltpath gt
-Dltname gt Define the symbol ltname gt
-output ltname gt Write output toltnamegtmexlowast
Modeling and Simulation Matlab 28
Makefile example MEX = mex
MEXOPT = -O -largeArrayDims
MEXLIBS = -lmwlapack
MEXDEBUG = -g -DDEBUG
RM = rm -f
Rules for targets
default all
all lapackLU clean
lapackLU
echo Compile MEX-file
$(MEX) $(MEXOPT) -output $ lapackLUc $(MEXLIBS)
echo Done
debug
echo Compile MEX-file for debugging$(MEX) $(MEXDEBUG) -output lapackLU lapackLUc $(MEXLIBS)
echo Done
clean
$(RM) o
Makefile for lapackLUc
Targets
lapackLU Create the mex file lapackLUmex
debug Compile mex-file with debug
information and trace outputs clean Remove all object files
Needed before compiling for a new
architecture
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 89
Modeling and Simulation Matlab 29
For more information
Go to Matlab helpMatlab External Interfaces
Or visithttpwwwmathworksseaccesshelpdeskhelptechdocmatlab_externalbp_kqh7html
Modeling and Simulation Matlab 30
Calling Java from Matlab
Intergrated in Matlab (since Matlab 6) Can call native (built-in) as well as external
Java classes directly from the Matlabcommand prompt
The Java classes do not need to be
compiled explicitly for Matlab No gateway function needed in the Java
class
Modeling and Simulation Matlab 31
The other way around
Possible to call Matlab functions from an
external programUNIX Through pipes
Windows through a COM interface
Use Matlab as computation engine
Modeling and Simulation Matlab 32
Component Object Model
Component Object Model (COM) ndash
Framework for integrating existingsoftware into an application
Software can be written in any language thatsupports COM
The applications communicate through aCOM object
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 99
Modeling and Simulation Matlab 33
Calling Matlab from Java
Is officially not supported by Mathworks
However
jmijar included to support this functionality
Third-party Java-Matlab interfaces exist
Modeling and Simulation Matlab 34
Compiled Matlab code
Compile your Matlab code to stand-aloneapplications or software
Matlab is not needed by the end-user
Requirements
Matlab Compiler
An ANSI C or C++ compiler
Restrictions Do not work with all toolboxes (or parts of)
Most pre-built GUIs can not be included
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 79
Modeling and Simulation Matlab 25
lapackLU testgtgt A = rand(5)
gtgt [LUP] = lapackLU(A)
L =
10000 0 0 0 0
03098 10000 0 0 0
05177 05538 10000 0 0
01236 00475 00414 10000 0
04995 09573 09230 -06548 10000
U =
07803 01320 02348 01690 05470
0 09153 07485 06794 05753
0 0 -05207 01840 -04128
0 0 0 03902 06089
0 0 0 0 02522
P =
1 0 0 0 0
0 0 0 0 1
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
gtgt norm(A-PLU)
ans = 12413e-16
Modeling and Simulation Matlab 26
Mex configuration
The option file for mex is mexoptssh allocated in (search order)Current directory
Matlabroot bin
The local preferences directory (returned byprefdir ) On Linux usually in
rsquoYour home directory rsquomatlab Matlabversion To configure mex (eg to select compiler)
type rsquomex -setup rsquo in Matlab
Modeling and Simulation Matlab 27
Mex options
-v Verbose
-Iltpath gt Search for header files inltpath gt
-lltlibrary gt Link with library ltlibrary gt
-Lltpath gt Search for libraries in ltpath gt
-Dltname gt Define the symbol ltname gt
-output ltname gt Write output toltnamegtmexlowast
Modeling and Simulation Matlab 28
Makefile example MEX = mex
MEXOPT = -O -largeArrayDims
MEXLIBS = -lmwlapack
MEXDEBUG = -g -DDEBUG
RM = rm -f
Rules for targets
default all
all lapackLU clean
lapackLU
echo Compile MEX-file
$(MEX) $(MEXOPT) -output $ lapackLUc $(MEXLIBS)
echo Done
debug
echo Compile MEX-file for debugging$(MEX) $(MEXDEBUG) -output lapackLU lapackLUc $(MEXLIBS)
echo Done
clean
$(RM) o
Makefile for lapackLUc
Targets
lapackLU Create the mex file lapackLUmex
debug Compile mex-file with debug
information and trace outputs clean Remove all object files
Needed before compiling for a new
architecture
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 89
Modeling and Simulation Matlab 29
For more information
Go to Matlab helpMatlab External Interfaces
Or visithttpwwwmathworksseaccesshelpdeskhelptechdocmatlab_externalbp_kqh7html
Modeling and Simulation Matlab 30
Calling Java from Matlab
Intergrated in Matlab (since Matlab 6) Can call native (built-in) as well as external
Java classes directly from the Matlabcommand prompt
The Java classes do not need to be
compiled explicitly for Matlab No gateway function needed in the Java
class
Modeling and Simulation Matlab 31
The other way around
Possible to call Matlab functions from an
external programUNIX Through pipes
Windows through a COM interface
Use Matlab as computation engine
Modeling and Simulation Matlab 32
Component Object Model
Component Object Model (COM) ndash
Framework for integrating existingsoftware into an application
Software can be written in any language thatsupports COM
The applications communicate through aCOM object
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 99
Modeling and Simulation Matlab 33
Calling Matlab from Java
Is officially not supported by Mathworks
However
jmijar included to support this functionality
Third-party Java-Matlab interfaces exist
Modeling and Simulation Matlab 34
Compiled Matlab code
Compile your Matlab code to stand-aloneapplications or software
Matlab is not needed by the end-user
Requirements
Matlab Compiler
An ANSI C or C++ compiler
Restrictions Do not work with all toolboxes (or parts of)
Most pre-built GUIs can not be included
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 89
Modeling and Simulation Matlab 29
For more information
Go to Matlab helpMatlab External Interfaces
Or visithttpwwwmathworksseaccesshelpdeskhelptechdocmatlab_externalbp_kqh7html
Modeling and Simulation Matlab 30
Calling Java from Matlab
Intergrated in Matlab (since Matlab 6) Can call native (built-in) as well as external
Java classes directly from the Matlabcommand prompt
The Java classes do not need to be
compiled explicitly for Matlab No gateway function needed in the Java
class
Modeling and Simulation Matlab 31
The other way around
Possible to call Matlab functions from an
external programUNIX Through pipes
Windows through a COM interface
Use Matlab as computation engine
Modeling and Simulation Matlab 32
Component Object Model
Component Object Model (COM) ndash
Framework for integrating existingsoftware into an application
Software can be written in any language thatsupports COM
The applications communicate through aCOM object
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 99
Modeling and Simulation Matlab 33
Calling Matlab from Java
Is officially not supported by Mathworks
However
jmijar included to support this functionality
Third-party Java-Matlab interfaces exist
Modeling and Simulation Matlab 34
Compiled Matlab code
Compile your Matlab code to stand-aloneapplications or software
Matlab is not needed by the end-user
Requirements
Matlab Compiler
An ANSI C or C++ compiler
Restrictions Do not work with all toolboxes (or parts of)
Most pre-built GUIs can not be included
7252019 Matlab Mex Handouts
httpslidepdfcomreaderfullmatlab-mex-handouts 99
Modeling and Simulation Matlab 33
Calling Matlab from Java
Is officially not supported by Mathworks
However
jmijar included to support this functionality
Third-party Java-Matlab interfaces exist
Modeling and Simulation Matlab 34
Compiled Matlab code
Compile your Matlab code to stand-aloneapplications or software
Matlab is not needed by the end-user
Requirements
Matlab Compiler
An ANSI C or C++ compiler
Restrictions Do not work with all toolboxes (or parts of)
Most pre-built GUIs can not be included