×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Matlab & C compilation

Matlab & C compilation

Matlab & C compilation

(OP)
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...

RE: Matlab & C compilation

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.

RE: Matlab & C compilation

(OP)
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...

RE: Matlab & C compilation

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;
  }
 }

RE: Matlab & C compilation

(OP)
Thanks a lot Mr.Sompting Guy.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources