Read book by duane hanselman and bruce littlefield "mastering MATLAB 7 ".
FIRST open MAT.file by using matOpen.
use matGetFp to get ANSI C file pointer to a MAT-file.
Select File -> Preferences -> Editor/Debugger -> Language -> C/C++ to specify preferences for editing C or C++ language files.
The Components of a C MEX-FileThe source code for a MEX-file consists of two distinct parts: A computational routine that contains the code for performing the computations that you want implemented in the MEX-file. Computations can be numerical computations as well as inputting and outputting data. A gateway routine that interfaces the computational routine with MATLAB by the entry point mexFunction and its parameters prhs, nrhs, plhs, nlhs, where prhs is an array of right-hand input arguments, nrhs is the number of right-hand input arguments, plhs is an array of left-hand output arguments, and nlhs is the number of left-hand output arguments. The gateway calls the computational routine as a subroutine. In the gateway routine, you can access the data in the mxArray structure and then manipulate this data in your C computational subroutine. For example, the expression mxGetPr(prhs[0]) returns a pointer of type double * to the real data in the mxArray pointed to by prhs[0]. You can then use this pointer like any other pointer of type double * in C. After calling your C computational routine from the gateway, you can set a pointer of type mxArray to the data it returns. MATLAB is then able to recognize the output from your computational routine as the output from the MEX-file. The following C MEX Cycle figure shows how inputs enter a MEX-file, what functions the gateway routine performs, and how outputs return to MATLAB
The two components of the MEX-file may be separate or combined. In either case, the files must contain the #include "mex.h" header so that the entry point and interface routines are declared properly. The name of the gateway routine must always be mexFunction and must contain these parameters.
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
/* more C code ... */
The parameters nlhs and nrhs contain the number of left- and right-hand arguments with which the MEX-file is invoked. In the syntax of the MATLAB language, functions have the general form [a,b,c,...] = fun(d,e,f,...)
where the ellipsis (...) denotes additional terms of the same format. The a,b,c,... are left-hand arguments and the d,e,f,... are right-hand arguments. The parameters plhs and prhs are vectors that contain pointers to the left- and right-hand arguments of the MEX-file. Note that both are declared as containing type mxArray *, which means that the variables pointed at are MATLAB arrays. prhs is a length nrhs array of pointers to the right-hand side inputs to the MEX-file, and plhs is a length nlhs array that will contain pointers to the left-hand side outputs that your function generates. For example, if you invoke a MEX-file from the MATLAB workspace with the command x = fun(y,z);
the MATLAB interpreter calls mexFunction with the arguments.
You can find the most recent versions of the example programs at the anonymous FTP serverftp://ftp.mathworks.com/pub/tech-support/docexamples/apiguide/R12/refbook
A First Example -- Passing a ScalarLet's look at a simple example of C code and its MEX-file equivalent. Here is a C computational function that takes a scalar and doubles it. #include
void timestwo(double y[], double x[])
{
y[0] = 2.0*x[0];
return;
}
Below is the same function written in the MEX-file format. /*
* =============================================================
* timestwo.c - example found in API guide
*
* Computational function that takes a scalar and doubles it.
*
* This is a MEX-file for MATLAB.
* Copyright (c) 1984-2000 The MathWorks, Inc.
* =============================================================
*/
/* $Revision: 1.8 $ */
#include "mex.h"
void timestwo(double y[], double x[])
{
y[0] = 2.0*x[0];
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs,
const mxArray *prhs[])
{
double *x, *y;
int mrows, ncols;
/* Check for proper number of arguments. */
if (nrhs != 1) {
mexErrMsgTxt("One input required.");
} else if (nlhs > 1) {
mexErrMsgTxt("Too many output arguments");
}
/* The input must be a noncomplex scalar double.*/
mrows = mxGetM(prhs[0]);
ncols = mxGetN(prhs[0]);
if (!mxIsDouble(prhs[0]) || mxIsComplex(prhs[0]) ||
!(mrows == 1 && ncols == 1)) {
mexErrMsgTxt("Input must be a noncomplex scalar double.");
}
/* Create matrix for the return argument. */
plhs[0] = mxCreateDoubleMatrix(mrows,ncols, mxREAL);
/* Assign pointers to each input and output. */
x = mxGetPr(prhs[0]);
y = mxGetPr(plhs[0]);
/* Call the timestwo subroutine. */
timestwo(y,x);
}
In C, function argument checking is done at compile time. In MATLAB, you can pass any number or type of arguments to your M-function, which is responsible for argument checking. This is also true for MEX-files. Your program must safely handle any number of input or output arguments of any supported type. To compile and link this example source file at the MATLAB prompt,
.jpeg)