MATLAB can call a C function, passing data to the function, and receiving results from the C function.
Of course, a MATLAB call normally looks something like
[ out1, out2 ] = function_name ( in1, in2, in3 )so in order to pass the information to and from MATLAB, the C function is required to have a header like this:
mexFunction ( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )where nlhs counts the number of output arguments, and plhs is a set of pointers to those arguments, with nrhs and prhs being the analogous quantities for the input arguments.
The user C file will need to invoke the mex.h include file, so that all the necessary MEX library functions and constants are declared.
The MATLAB Mex library includes a number of operators to extract information from the MATLAB input arguments, and to store the computed results into the output arguments. Information about these functions does not seem to be available online, and instead must be discovered from examples and the MATLAB manuals. I am referring here to functions such as
Becuase MATLAB is in control, you have to be careful in your C code. For instance, you aren't supposed to use malloc and free for memory management, but rather mxCalloc and mxFree. Also, you aren't supposed to used printf for output, but rather mexPrintf. (I have accidentally broken both these rules, and I'm not sure how serious the consequences are, if any!)
Once the C file is written, it must be "compiled" with the MATLAB MEX compiler, which will, as part of its work, also invoke some C compiler on your machine. The first time you use the MEX compiler, you may need to tell it where the appropriate compiler is, or which compiler to use. You can also, at any time, request that MEX switch to a different compiler, if there is a choice. (For instance, you may be able to choose between your computer's default "cc" compiler and the GNU "gcc" compiler.) To choose or change your compiler, type (inside of MATLAB)
mex -seteup
Assuming your C file is called fact.c, for example, you can process it through the MEX compiler. You can always issue this command inside of MATLAB, and on some systems, the MEX compiler may be availabe directly as an external command. In either case, you issue the command:
mex fact.cwhich creates a compiled file whose name is system-dependent. On a Windows PC, it might be called fact.dll, and on a UNIX system, it might be something like fact.mexglx. In any case, the compiled file behaves essentially like a MATLAB M file called fact.m (except that it should, you hope, execute much faster than a MATLAB script).
In the very likely event that you have problems compiling the code, or using it from MATLAB, you may want to request the verbose compilation, with symbolic information added for debugging:
mex -v -g fact.c
F90_MATLAB contains examples in which a FORTRAN90 program issues a call to MATLAB to carry out an auxillary calculation.
MATLAB contains some simple examples of MATLAB usage.
MATLAB_BATCH contains examples in which MATLAB is used in "batch mode", that is, noninteractively.
MATLAB_F77 contains examples in which MATLAB can call a FORTRAN77 function, using the MEX facility.
MATLAB_MOVIES contains examples in which MATLAB is used to generate animations.
MATLAB_OS contains examples in which MATLAB can invoke an operating system command.
MIXED contains examples in which a C main program calls routines in other programming languages, and vice versa.
NSASM is a library of C routines, intended to be used with a MATLAB calling program, and which set up the sparse matrix needed for a Newton iteration to solve a finite element formulation of the steady incompressible 2D Navier Stokes equations.
FACT is an example in which a C function is used to compute the factorial function. A single routine is used, which takes care of the "translation" between MATLAB and C, and the computation of the result.
CHEBY_U is an example in which a C function is used to compute the Chebyshev U polynomial. The coding is done in such a way that the computation is in a separate C function; the mexFunction is only used to "translate" between MATLAB and C.
You can go up one level to the MATLAB source codes.