Connecting C and Matlab
Matlab has a facility for allowing you to run C programs within
matlab, and have them take as input and return as output matlab
data types.
For one simple example, see
mex_speedTest.c.
The basic rules are:
- Instead of main, you have a function called
mexFunction.
- You use special routines to read and construct
matlab data types.
These usually have names beginning with mx or mex.
For documentation on them, you should examine
- It is preferred that you use Matlab's routines for allocating
and freeing memory: mxCalloc, mxMalloc and mxFree.
- Include "mex.h" in the file. Don't worry about where it is, and
the matlab mex compiler should find it on your system.
- To compile, type mex followed by the filename.
For example, I typed mex mex_speedTest.c.
This creates mex_speedTest.mexglx (in linux), which
is the file that matlab will call when you type mex_speedTest.
I recommend the following examples of mex C code:
- explore.c: code from Mathworks that
demonstrates how to process every type of input
- RankMod2.c: code we will use
later in the semester for computing the rank of a matrix
over GF(2).
- FormDualMod2.c: code we will use
later in the semester for computing the
dual of a matrix over {0,1}.
Here's an example of compiling and using all three.
Before I begin, let me warn you that RankMod2 and FormDualMod2 use
the matlab logical type to save space when storing 0/1 data.
Early versions of Matlab do not have this data type.
mex explore.c
explore
explore(1)
------------------------------------------------
Name: prhs[0]
Dimensions: 1x1
Class Name: double
------------------------------------------------
(1,1) = 1
explore(1,2,3)
------------------------------------------------
Name: prhs[0]
Dimensions: 1x1
Class Name: double
------------------------------------------------
(1,1) = 1
------------------------------------------------
Name: prhs[1]
Dimensions: 1x1
Class Name: double
------------------------------------------------
(1,1) = 2
------------------------------------------------
Name: prhs[2]
Dimensions: 1x1
Class Name: double
------------------------------------------------
(1,1) = 3
x = rand(3)
x =
0.53408 0.8385 0.70274
0.72711 0.56807 0.54657
0.30929 0.37041 0.44488
y.a = 1; y.b = 'hi there';
z{1} = 1; z{2} = 'hi there';
explore(x,y,z)
------------------------------------------------
Name: prhs[0]
Dimensions: 3x3
Class Name: double
------------------------------------------------
(1,1) = 0.534079
(2,1) = 0.727113
(3,1) = 0.30929
(1,2) = 0.838496
(2,2) = 0.568072
(3,2) = 0.370414
(1,3) = 0.70274
(2,3) = 0.546571
(3,3) = 0.44488
------------------------------------------------
Name: prhs[1]
Dimensions: 1x1
Class Name: struct
------------------------------------------------
(1,1).a
------------------------------------------------
Dimensions: 1x1
Class Name: double
------------------------------------------------
(1,1) = 1
(1,1).b
------------------------------------------------
Dimensions: 1x8
Class Name: char
------------------------------------------------
(1,1) hi there
------------------------------------------------
Name: prhs[2]
Dimensions: 1x2
Class Name: cell
------------------------------------------------
total num of cells = 2
Cell Element: (1,1)
------------------------------------------------
Dimensions: 1x1
Class Name: double
------------------------------------------------
(1,1) = 1
Cell Element: (1,2)
------------------------------------------------
Dimensions: 1x8
Class Name: char
------------------------------------------------
(1,1) hi there
mex RankMod2.c
a = round(rand(4,8))
a =
1 1 0 0 0 0 1 1
1 1 0 0 1 1 0 1
1 0 1 1 0 1 0 1
1 1 1 0 0 0 0 1
RankMod2(a)
??? Usage: input must be a logical matrix
Error in ==> /home/r1/spielman/www/ECC/cc/RankMod2.mexglx
a = rand(4,8) > 1/2
a =
0 0 1 1 1 0 1 0
0 1 0 0 1 1 0 1
1 1 0 1 0 0 1 1
1 0 0 1 0 1 0 0
RankMod2(a)
ans =
4
a = rand(4,4) > 1/2
a =
1 1 0 0
1 1 0 0
0 0 0 1
0 1 0 0
RankMod2(a)
ans =
3
a = rand(4,8) > 1/2
a =
0 0 0 0 0 0 1 1
1 0 1 0 1 1 1 0
1 1 1 0 1 0 0 1
0 0 1 0 1 0 0 1
mex FormDualMod2.c
b = FormDualMod2(a)
b =
0 0 1 0
0 0 1 0
0 1 0 1
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
0 0 0 1
explore(b)
------------------------------------------------
Name: prhs[0]
Dimensions: 8x4
Class Name: logical
------------------------------------------------
diary off
Dan Spielman
Last modified: Tue Feb 10 16:46:51 EST 2004