Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Compiling C code to MEX file - an example from help doesnt work

124 views
Skip to first unread message

r.p.m...@googlemail.com

unread,
Dec 3, 2008, 11:48:46 AM12/3/08
to
Hi,
I have some C code to be run from already written Matlab program.
I tried to follow example from help: Examples of C Source MEX-Files: A
First Example — Passing a Scalar.
It compiles fine.
I try to run example as it is described in help:

x = 2;
y = timestwo(x)
y =
4

and I experience the following error:

??? Error using ==> timestwo
MEX level2 S-function "timestwo" must be called with at least 4 right
hand arguments

So I try to be clever and put more arguments to function:
y = timestwo(1,1,0,0) and it

I use Matlab version:
MATLAB Version 7.6.0.324 (R2008a)
Operating System: Microsoft Windows XP Version 5.1 (Build 2600:
Service Pack 3)
Java VM Version: Java 1.6.0 with Sun Microsystems Inc. Java HotSpot
(TM) Client VM mixed mode

And here is code from example from Matlab that I am trying to run:

#include "mex.h"

/*
* timestwoalt.c - example found in API guide
*
* use mxGetScalar to return the values of scalars instead of pointers
* to copies of scalar variables.
*
* This is a MEX-file for MATLAB.
* Copyright 1984-2000 The MathWorks, Inc.
*/

/* $Revision: 1.6 $ */

void timestwo_alt(double *y, double x)
{
*y = 2.0*x;
}

void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] )
{
double *y;
double x;

/* Check arguments */

if (nrhs != 1) {
mexErrMsgTxt("One input argument required.");
} else if (nlhs > 1) {
mexErrMsgTxt("Too many output arguments.");
} else if (!mxIsNumeric(prhs[0])) {
mexErrMsgTxt("Argument must be numeric.");
} else if (mxGetNumberOfElements(prhs[0]) != 1 || mxIsComplex(prhs
[0])) {
mexErrMsgTxt("Argument must be non-complex scalar.");
}
/* create a 1-by-1 matrix for the return argument */
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);

/* get the scalar value of the input x */
/* note: mxGetScalar returns a value, not a pointer */
x = mxGetScalar(prhs[0]);

/* assign a pointer to the output */
y = mxGetPr(plhs[0]);

/* call the timestwo_alt subroutine */
timestwo_alt(y,x);
}

Regards
Ryszard

Rune Allnor

unread,
Dec 3, 2008, 12:23:43 PM12/3/08
to
On 3 Des, 17:48, "r.p.mac...@googlemail.com"

<r.p.mac...@googlemail.com> wrote:
> Hi,
> I have some C code to be run from already written Matlab program.
> I tried to follow example from help: Examples of C Source MEX-Files: A
> First Example — Passing a Scalar.
> It compiles fine.

What compiler and system etc do you use?

Rune

r.p.m...@googlemail.com

unread,
Dec 3, 2008, 12:48:32 PM12/3/08
to
I have tried both:
[1] Lcc-win32 C 2.4.1 in C:\PROGRA~1\MATLAB\R2008a\sys\lcc\bin
[2] Microsoft Visual C++ 2005 in C:\Program Files\Microsoft Visual
Studio 8

As I have written in my query I use Matlab version:


MATLAB Version 7.6.0.324 (R2008a)
Operating System: Microsoft Windows XP Version 5.1 (Build 2600:
Service Pack 3)
Java VM Version: Java 1.6.0 with Sun Microsystems Inc. Java HotSpot
(TM) Client VM mixed mode

Steven Lord

unread,
Dec 3, 2008, 1:23:53 PM12/3/08
to

<r.p.m...@googlemail.com> wrote in message
news:7b121f62-26c6-4eca...@w35g2000yqm.googlegroups.com...

> Hi,
> I have some C code to be run from already written Matlab program.
> I tried to follow example from help: Examples of C Source MEX-Files: A
> First Example — Passing a Scalar.
> It compiles fine.
> I try to run example as it is described in help:
>
> x = 2;
> y = timestwo(x)
> y =
> 4
>
> and I experience the following error:
>
> ??? Error using ==> timestwo
> MEX level2 S-function "timestwo" must be called with at least 4 right
> hand arguments

Is the name of the MEX-file that you created timestwo.mexw32 or
timestwoalt.mexw32? There's an example C MEX S-function called timestwo,
and it looks like MATLAB thinks you're trying to invoke that S-function
rather the MEX-file you built:

http://www.mathworks.com/access/helpdesk/help/toolbox/simulink/sfg/f8-94774.html

It looks like the basic example in the MEX documentation is also called
timestwo, and I think that could probably cause confusion. I'll mention
that to the MEX documentation staff.

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/f23674.html

As the instructions at the bottom of the page states, you might want to copy
the timestwo example you're working on to a work folder (and perhaps call it
timestwoalt), as then even if you call it timestwo just like the S-function,
since it's in the current directory it will be used instead of the
S-function version.

*snip*

--
Steve Lord
sl...@mathworks.com


r.p.m...@googlemail.com

unread,
Dec 3, 2008, 2:03:25 PM12/3/08
to
Thank you !
Thats it indeed! I should swear in this place ;) Definitely it should
be changed in documentation or at least should be said.

Although I copied a code from timestwoalt I was compiling and using
timestwo as timestwo.

Now I understand most of my mistakes, but thats only the beginning of
my path because I am willing to be able to run my c++ code (ie
exchange data) with structures and classes from matlab as well as from
some free 3rd part stuff, so wish me good luck

Thanks again
Ryszard

On Dec 3, 6:23 pm, "Steven Lord" <sl...@mathworks.com> wrote:
> <r.p.mac...@googlemail.com> wrote in message


>
> news:7b121f62-26c6-4eca...@w35g2000yqm.googlegroups.com...
>
>
>
> > Hi,
> > I have some C code to be run from already written Matlab program.
> > I tried to follow example from help: Examples of C Source MEX-Files: A
> > First Example — Passing a Scalar.
> > It compiles fine.
> > I try to run example as it is described in help:
>
> > x = 2;
> > y = timestwo(x)
> > y =
> > 4
>
> > and I experience the following error:
>
> > ??? Error using ==> timestwo
> > MEX level2 S-function "timestwo" must be called with at least 4 right
> > hand arguments
>
> Is the name of the MEX-file that you created timestwo.mexw32 or
> timestwoalt.mexw32? There's an example C MEX S-function called timestwo,
> and it looks like MATLAB thinks you're trying to invoke that S-function
> rather the MEX-file you built:
>

> http://www.mathworks.com/access/helpdesk/help/toolbox/simulink/sfg/f8...


>
> It looks like the basic example in the MEX documentation is also called
> timestwo, and I think that could probably cause confusion. I'll mention
> that to the MEX documentation staff.
>

> http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external...

0 new messages