MATLAB External Interfaces / API (Fujinaga)
Giude
Reference
Calling C functions from MATLAB
MEX-files (as opposed to m-files)
-
dynamically linked C functions
-
extensions: linux (.mexglx) windows (.dll)
-
MATALAB uses mex-files, e.g., conv2.mex
-
mx
functions - mxArrays related functions
-
mex
functions - perform operations back in MATLAB environment
MATALB array
-
The MATLAB language works with only a single object type: the MATLAB array.
-
All MATLAB variables, including scalars, vectors, matrices, strings, cell
arrays, structures, and objects are stored as MATLAB arrays.
-
In C, the MATLAB array is declared to be of type mxArray.
-
The mxArray structure contains, among other things:
-
Its type
-
Its dimensions
-
The data associated with this array
-
If numeric, whether the variable is real or complex
-
If sparse, its indices and nonzero maximum elements
-
If a structure or object, the number of fields
and field names
-
All MATLAB data is stored columnwise (Fortarn legacy). e.g.:
a = ['linux'; 'perl
'; 'gimp ']
size(a) -> 3x5
stored as: [l p g i
e i n r m u l p x ]
Data types
-
Complex double-precision
-
Numeric: single precision float, 8-, 16-, 32-bit ints, signed and unsigned
-
Strings: 16-bit ASCII Unicode (not null-terminated)
-
Sparce matrix: pr, pi, nzmax, ir, & jc
-
nzmax - max
number of nonzero elements in matrix
-
ir - integer
array of length nzmax containing row indeixes of elements in pr &
pi.
-
jc - integer
array of length N+1 containing column index information. jc[j] is the index
in ir and pr of the first nonzerop entry in the jth column. (See Tutorial
below for an example).
-
Cell arrays - collection of MATLAB arrays
-
Structures
-
Objects
cd MATLAB
x = 2;
explore(x);
explore([1 2 3 4 5])
explore 1 2 3 4 5
explore({1 2 3 4 5})
explore(int8([1 2 3
4 5]))
explore {1 2 3 4 5}
explore(sparse(eye(5)))
explore(struct('name',
'Joe Jones', 'ext', 7332))
explore(1, 2, 3, 4,
5)
Building MEX-files
Calling MATLAB functions from C mex-file
mexCallMATLAB
e.g. sincall.c
Misc.
-
Help file (use m-file)
-
LAPACK, BLAS (Fortran linear algebra subroutines)
-
C++ MEX-files are only supported to the extent that the compiler supports
C++ shared libraries
-
C++ MEX-files are only supported for the version of the compiler that MATLAB
was linked with.
-
C++
MEX-file example
-
Calling
MATLAB from C programs
Tutorial