How to pass arguments in a mex file with a "main" function?

6 views (last 30 days)
Hi everyone,
I am trying to run an existing program written in C, using mex. I am new to mex functions. My existing C program has a main function of the form
int main( int argc, char **argv )
{
< Parse inputs >
< Call other functions that do calculations >
< Return answer >
}
Normally, this program takes three inputs from the command line (a string and two integers), which are stored in argv. What is the proper syntax for generating and passing these arguments in my mexFunction? I tried
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
size_t buflen = mxGetN(prhs[0])*sizeof(mxChar)+1;
char *filename = mxMalloc(buflen);
mxGetString(prhs[0], filename, (mwSize)buflen);
double param1 = mxGetScalar(prhs[1]);
double param2 = mxGetScalar(prhs[2]);
main(filename, param1, param2);
}
but it won't compile and gives the following errors:
src/main.c:110: warning: passing argument 1 of 'main' makes integer from pointer without a cast
src/main.c:110: error: incompatible type for argument 2 of 'main'
src/main.c:110: error: too many arguments to function 'main'
Thanks for any advice!
  1 Comment
Dan
Dan on 9 Jan 2014
P.S. I suppose I could try to modify the main function to take different inputs, but I don't know if this is a good idea (from the little I know about C programs, it seems like the inputs to main are always the same).

Sign in to comment.

Answers (1)

Matt J
Matt J on 9 Jan 2014
In addition to modifying the input signature to accept
(char * , double, double)
I suspect that you have to rename your main function to something else. In C/C++, main() is reserved for the topmost level function whereas mex files that you run in MATLAB are always subroutines to some larger C/C++ project in which MATLAB itself runs.

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!