Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Matlab & C compilation

Status
Not open for further replies.

shivas007

Bioengineer
Sep 11, 2005
8
Hi,
I want some help regarding the calling of C routines in matlab(.m) files. I wrote a function in C and im calling it from a '.m' file and using its return type. My question here is as how to implement this procedure. I know a little bit about using mex, mcc in matlab but dont know exactly as how the actual procedural implementation should be. It will be a great help if you guys can please provide me some information regarding the implementation. Thanks...
 
Replies continue below

Recommended for you

Can you be more specific? Do you understand C and can you get the deleivered C-MEX examples to build and work? I'm not sure I understand the phrase: "how the actual procedural implementation should be". Surely it's whatever you want it to be.
 
sorry about that.Here is what im doing,I have written a program in 'C' which reads a file(say 'getvalues.txt') and returns some variable values(example:returns a,b,c,d variable values from this file, ). I have to use these values in a matlab program. So, I was thinking to call this 'C' program (example: filreader.c) from the matlab program (example: size.m) and use its return type (i.e, values a,b,c,d) in the matlab program. So, its just a function call to a 'C' program from a '.m' file. I haven't worked on C-MEX before and i don't know anything about its implementation, as what are the necessary headers to include( in both 'C' & '.m' programs), and how the compilation should be.It will be a great help if you guys can please help me here, Thanks...
 
Matlab can call your C code via a special gateway. The C function below is a really simple example, but it shows the required bits. YOu have to provide a function called mexFunction() with the prototype shown below. The include file: mex.h contains all the typedefs for the Matlab structures used in the code. Simply put, you get pointers to the input arguments. You dig the data out of them. Then you create return arguments and fill them up. To build the file:

>> mex yourname.c

This creates a function you can call directly.


/* A simple MEX file to calculate column means */

#include <mex.h>
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
int row,col;
int m=mxGetM(prhs[0]), n=mxGetN(prhs[0]);
double total, *y, *x = mxGetPr(prhs[0]);

/* Special case for row vector */
if(m==1)
{
m=n;
n=1;
}

/* Create output matrix */
plhs[0] = mxCreateDoubleMatrix(1, n , mxREAL);
y = mxGetPr(plhs[0]);

/* Work Loop */
for(col=0; col<n; col++)
{
for(row=0, total=0; row<m; row++) total += *x++;
y[col] = total/m;
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor