From: Travis on
Hi,

I have a lot of .c files that I would like to access
directly from the matlab workspace. From what I have been
reading, it seems that a mex file creates the matlab
executable to do this.

Do I have to put specific commands in my existing c-code or
should I be able to simply run "mex c_filename.c" and it
will create it automatically?

I have added #include "mex.h" to the source code and got the
following error:
Specified export _mexFunction is not defined
Missing exports. Aborting

This prompted me to try and add the following (as I have
seen in documentation):

mexFunction(int nlhs, mxArray *plhs[], int nrhs, const
mxArray *prhs[])

But then I get more errors.

I am not a c-programmer so I am not sure what this means
(the code was given to me and I was asked to access it from
matlab... or translate all of the source files to m-files,
but i think that would take even longer). Do i need to
redefine variables, functions, calls, (or whatever else) in
each source code file so they say mx...whatever or does the
call to "mex" do this automatically? Isn't the purpose of
that command to simply create the wrapper for the c-code
without modifying any of it or am I mistaken?

I've looked at the documentation on the site and the Matlab
external interfaces book itself (which is essentially the
same) but I am still confused.

Any direction/suggestions would be helpful (especially
regarding my specific questions regarding what to change in
the c-files before trying to mex them, if anything)

Thanks,
Travis
From: Xavier Serrand on

"Travis " <traviib.nospam.(a)yahoo.com> a �crit dans le message de
news:fcmr2v$5iv$1(a)fred.mathworks.com...
> Hi,
>
> I have a lot of .c files that I would like to access
> directly from the matlab workspace. From what I have been
> reading, it seems that a mex file creates the matlab
> executable to do this.
>
> Do I have to put specific commands in my existing c-code or
> should I be able to simply run "mex c_filename.c" and it
> will create it automatically?
>
> I have added #include "mex.h" to the source code and got the
> following error:
> Specified export _mexFunction is not defined
> Missing exports. Aborting
>
> This prompted me to try and add the following (as I have
> seen in documentation):
>
> mexFunction(int nlhs, mxArray *plhs[], int nrhs, const
> mxArray *prhs[])
>
> But then I get more errors.
>
> I am not a c-programmer so I am not sure what this means
> (the code was given to me and I was asked to access it from
> matlab... or translate all of the source files to m-files,
> but i think that would take even longer). Do i need to
> redefine variables, functions, calls, (or whatever else) in
> each source code file so they say mx...whatever or does the
> call to "mex" do this automatically? Isn't the purpose of
> that command to simply create the wrapper for the c-code
> without modifying any of it or am I mistaken?
>
> I've looked at the documentation on the site and the Matlab
> external interfaces book itself (which is essentially the
> same) but I am still confused.
>
> Any direction/suggestions would be helpful (especially
> regarding my specific questions regarding what to change in
> the c-files before trying to mex them, if anything)
>
> Thanks,
> Travis

mex compiles your c files into DLL. (dynamic linked librarie)
Then you can call your function as if it were native matlab

In order to build a correct DLL, you have to declare an entry point :
void mexFunction(int nlhs, /* number of expected outputs
*/
mxArray *plhs[], /* array of pointers to
output arguments */
int nrhs, /* number of inputs
*/
const mxArray *prhs[]);/* array of pointers to
input arguments */

Into the body of this fonction, you can call c routines ...
Job to do unwrap and wrap arguments in, out : see help in
order to cinvert mxArray into c arrays

The results of your c routines must be passed as an element of plhs.
If you don't want to return some values, you can use any c functions
as printf or fwrite in order to output some values

Xavier