f



matlab mex file

I want to create a mex file to be called by matlab. I have done all
the basic stuff and got the examples to work. But now I want to
create a much more ellaborate external c program. But all my calles
to things like "strcta" or "CreateFile" which works well in MSVC6.0
fials to compile and link here.
How do I do this?
Thanks
Henry
0
tehrani (3)
8/24/2005 9:17:01 PM
comp.soft-sys.matlab 211266 articles. 24 followers. lunamoonmoon (257) is leader. Post Follow

4 Replies
637 Views

Similar Articles

[PageSpeed] 18

henry terhani wrote:
> I want to create a mex file to be called by matlab. I have done all
> the basic stuff and got the examples to work. But now I want to
> create a much more ellaborate external c program. But all my calles
> to things like "strcta" or "CreateFile" which works well in MSVC6.0
> fials to compile and link here.
> How do I do this?
> Thanks
> Henry

Provide specific examples of failures.
0
8/24/2005 9:15:38 PM
Hello:
The failue is in the link stage. It complains about not finding the
external file
_CreateFile
So the link fails.
Also many of the definition in the headers fail to be found and I had
to manually add them in.
Henry

Christopher Hulbert wrote:
>
>
> henry terhani wrote:
>> I want to create a mex file to be called by matlab. I have done
> all
>> the basic stuff and got the examples to work. But now I want to
>> create a much more ellaborate external c program. But all my
> calles
>> to things like "strcta" or "CreateFile" which works well in
> MSVC6.0
>> fials to compile and link here.
>> How do I do this?
>> Thanks
>> Henry
>
> Provide specific examples of failures.
>
0
tehrani (3)
8/24/2005 9:34:50 PM
henry tehrani wrote:
>
>
> Hello:
> The failue is in the link stage. It complains about not finding the
> external file
> _CreateFile
> So the link fails.
> Also many of the definition in the headers fail to be found and I
> had
> to manually add them in.
> Henry
>
> Christopher Hulbert wrote:
>>
>>
>> henry terhani wrote:
>>> I want to create a mex file to be called by matlab. I have
> done
>> all
>>> the basic stuff and got the examples to work. But now I
want
> to
>>> create a much more ellaborate external c program. But all
my
>> calles
>>> to things like "strcta" or "CreateFile" which works well in
>> MSVC6.0
>>> fials to compile and link here.
>>> How do I do this?
>>> Thanks
>>> Henry
>>
>> Provide specific examples of failures.
>>

You must add several files & libraries to compile list
To begin use your file with:
>> mex myfile.c -v
So you will see what extra libraries ... you need to link or compile
0
R_karimiM (42)
8/25/2005 12:40:58 AM
here is my example I need to compile and run
#include "mex.h"

/*
 * timestwo.c - example found in API guide
 *
 * Computational function that takes a scalar and double it.
 *
 * This is a MEX-file for MATLAB.
 * Copyright 1984-2000 The MathWorks, Inc.
 */
 
/* $Revision: 1.8 $ */

bool
OpenDevice(/*Handle *phDeviceHandle,*/ char devname)
{
char completeDeviceName[64] = "";
char pcMsg[64] = "";
void *phDeviceHandle;

		strcat (completeDeviceName,
				"\\\\.\\"
				);

		strcat(completeDeviceName,
				devname
				);

		*phDeviceHandle = CreateFile( completeDeviceName,
										GENERIC_WRITE,
										FILE_SHARE_WRITE,
										NULL,
										OPEN_EXISTING,
										0,
										NULL);
/*
		if (*phDeviceHandle == INVALID_HANDLE_VALUE)
			AfxThrowUserException();
*/
return (true);

}

here is the result after doing
mex timestwo.c -v

This is mex, Copyright 1984-2002 The MathWorks, Inc.
 
-> Default options filename found in C:\Documents and
Settings\Tehrani\Application Data\MathWorks\MATLAB\R13
----------------------------------------------------------------
-> Options file = C:\Documents and
Settings\Tehrani\Application Data\MathWorks\MATLAB\R13\mexopts.bat
      MATLAB = C:\matlab_sv13
-> COMPILER = cl
-> Compiler flags:
         COMPFLAGS = -c -Zp8 -G5 -W3 -DMATLAB_MEX_FILE
-nologo
         OPTIMFLAGS = /MD -O2 -Oy- -DNDEBUG
         DEBUGFLAGS = /MDd -Zi -Fd"timestwo.pdb"
         arguments =
         Name switch = /Fo
-> Pre-linking commands =
-> LINKER = link
-> Link directives:
         LINKFLAGS = /dll /export:mexFunction /MAP
/LIBPATH:"C:\matlab_sv13\extern\lib\win32\microsoft\msvc60" libmx.lib
libmex.lib libmatlb.lib libmat.lib /implib:_lib2251.x
         LINKFLAGSPOST =
         Name directive = /out:"timestwo.dll"
         File link directive =
         Lib. link directive =
         Rsp file indicator = @
-> Resource Compiler = rc /fo "mexversion.res"
-> Resource Linker =
----------------------------------------------------------------
 
 
--> "cl -c -Zp8 -G5 -W3 -DMATLAB_MEX_FILE -nologo /Fotimestwo.obj
-IC:\matlab_sv13\extern\include -IC:\matlab_sv13\simulink\include /MD
-O2 -Oy- -DNDEBUG timestwo.c"
 
timestwo.c
timestwo.c(34) : error C2100: illegal indirection
timestwo.c(34) : warning C4013: 'CreateFile' undefined; assuming
extern returning int
timestwo.c(35) : error C2065: 'GENERIC_WRITE' : undeclared identifier

timestwo.c(36) : error C2065: 'FILE_SHARE_WRITE' : undeclared
identifier
timestwo.c(38) : error C2065: 'OPEN_EXISTING' : undeclared identifier

timestwo.c(40) : warning C4047: '=' : 'void *' differs in levels of
indirection from 'int '
 
  C:\MATLAB_SV13\BIN\WIN32\MEX.PL: Error: Compile of 'timestwo.c'
failed.
 
??? Error using ==> mex
Unable to complete successfully

What libraries or headers do I need to include for this ?
Thanks
Henry

void timestwo(double y[], double x[])
{
  y[0] = 3.5*x[0];
}

void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[] )
{
  double *x,*y;
  int mrows,ncols;
  
  /* Check for proper number of arguments. */
  if(nrhs!=1) {
    mexErrMsgTxt("One input required.");
  } else if(nlhs>1) {
    mexErrMsgTxt("Too many output arguments");
  }
  
  /* The input must be a noncomplex scalar double.*/
  mrows = mxGetM(prhs[0]);
  ncols = mxGetN(prhs[0]);
  if( !mxIsDouble(prhs[0]) || mxIsComplex(prhs[0]) ||
      !(mrows==1 && ncols==1) ) {
    mexErrMsgTxt("Input must be a noncomplex scalar double.");
  }
  
  /* Create matrix for the return argument. */
  plhs[0] = mxCreateDoubleMatrix(mrows,ncols, mxREAL);
  
  /* Assign pointers to each input and output. */
  x = mxGetPr(prhs[0]);
  y = mxGetPr(plhs[0]);
  
  /* Call the timestwo subroutine. */
  timestwo(y,x);
}

R. Karimi wrote:
>
>
> henry tehrani wrote:
>>
>>
>> Hello:
>> The failue is in the link stage. It complains about not finding
> the
>> external file
>> _CreateFile
>> So the link fails.
>> Also many of the definition in the headers fail to be found and
I
>> had
>> to manually add them in.
>> Henry
>>
>> Christopher Hulbert wrote:
>>>
>>>
>>> henry terhani wrote:
>>>> I want to create a mex file to be called by matlab. I
> have
>> done
>>> all
>>>> the basic stuff and got the examples to work. But now I
> want
>> to
>>>> create a much more ellaborate external c program. But
all
> my
>>> calles
>>>> to things like "strcta" or "CreateFile" which works
well
> in
>>> MSVC6.0
>>>> fials to compile and link here.
>>>> How do I do this?
>>>> Thanks
>>>> Henry
>>>
>>> Provide specific examples of failures.
>>>
>
> You must add several files & libraries to compile list
> To begin use your file with:
>>> mex myfile.c -v
> So you will see what extra libraries ... you need to link or compile
0
tehrani (3)
8/25/2005 6:46:53 PM
Reply: