Jump to content
Science Forums

Using c code in Matlab...on xubuntu


sanctus

Recommended Posts

I don't really like Matlab, but where I do civil service at the moment I have to use it. Luckily there is some extern c-code in it (at least it gets to almost acceptable speeds this way).

 

Now since there are strong security issues where I work I don't have admin rights on my machine, this implied that I installed Xubuntu today, which took me all day...

Now, to use the c-file you have to compile them out of Matlab via a comand

mex myfile.c

But before doing this you have to choose which compiler to use...I choose gcc (version 4.1.3 or 4.3.1 don't remember) also because the only available already installed.

 

The error I get is very strange because it says something like: "error: missing '(' or (...) before '/' token"

 

 

(...) stands for a part I don't remember now (I'm at home now and not at work)

and the C-code starts with some comments //

 

I think that there is something not installed in the gcc, but what is it?

 

Hope it was comprehensible

Link to comment
Share on other sites

So you don't think it is a compiler issue?

 

Because on windows it compiled fine so I don't think it is a code problem, but anyway here is the code

 

/* : 1.1 $ */
// Automatically generated by Matlab AppWizard version 1.0
//
// This is the gateway routine for a MATLAB Math/Graphics Library-based
// C MATLAB MEX File.


#include "mex.h"
#include <math.h>
#define K 10.0
#define MAX(A, ((A)>(:)?(A):(:hihi:)
#define MIN(A,:lol: ((A)<(:)?(A):(:))
#define SGN(A) ((A)>0?1.:-1.)
#define ABS(A) ((A)>0?(A):-(A))

// Computes |grad u| div[grad(u)/|grad(u)|]
//--------------------------------------------
void 	giveGradient(double *GpX,double *GpY,double *GpZ,
                   double *GpXpY,double *GpXmY,
                   double *GpYpZ,double *GmYpZ,double *GpXpZ,double *GmXpZ,
                   double *GpXpYpZ,double *GmXpYpZ,double *GpXmYpZ,double *GmXmYpZ,
                   double *Gradient,double *Laplacian, double *In, double *MASK, int* dim)
{	
int ix,iy,iz;
int rows, pagesize;
   int index,pX,pY,pZ;
   int pXpY,pXmY,pYpZ,mYpZ,pXpZ,mXpZ;
   int pXpYpZ, mXpYpZ,pXmYpZ, mXmYpZ;
   double cnst1 = 1.0/sqrt(2);
   double cnst2 = 1.0/sqrt(3);
   
   rows = dim[0];
pagesize = dim[0]*dim[1];
   
for(ix=1;ix<dim[0]-1;ix++) 
	for(iy=1;iy<dim[1]-1;iy++) 
		for(iz=1;iz<dim[2]-1;iz++) 
		{
			index   = ix + iy*rows + iz*pagesize;
               if(MASK[index]==0)
                   Gradient[index]=0;
               else
               {
                   // One connected pixels
                   pX = index+1;
                   pY = index+rows;
                   pZ = index+pagesize;
                   GpX[index] = MASK[pX]*(In[pX]-In[index]);
                   GpY[index] = MASK[pY]*(In[pY]-In[index]);
                   GpZ[index] = MASK[pZ]*(In[pZ]-In[index]);
                   
                   Gradient[index] = GpX[index]*GpX[index] + GpY[index]*GpY[index]  + GpZ[index]*GpZ[index];

                   // Two connected pixels

                   pXpY = index+rows+1;
                   pXmY = index-rows+1;
                   pYpZ = index+pagesize+rows;
                   mYpZ = index+pagesize-rows;
                   pXpZ = index+pagesize+1;
                   mXpZ = index+pagesize-1;

                   GpXpY[index] = cnst1*MASK[pXpY]*(In[pXpY]-In[index]);
                   GpXmY[index] = cnst1*MASK[pXmY]*(In[pXmY]-In[index]);
                   GpYpZ[index] = cnst1*MASK[pYpZ]*(In[pYpZ]-In[index]);
                   GmYpZ[index] = cnst1*MASK[mYpZ]*(In[mYpZ]-In[index]);
                   GpXpZ[index] = cnst1*MASK[pXpZ]*(In[pXpZ]-In[index]);
                   GmXpZ[index] = cnst1*MASK[mXpZ]*(In[mXpZ]-In[index]);

                   Gradient[index] += GpXpY[index]*GpXpY[index] + GpXmY[index]*GpXmY[index]  + GpYpZ[index]*GpYpZ[index];
                   Gradient[index] += GmYpZ[index]*GmYpZ[index] + GpXpZ[index]*GpXpZ[index]  + GmXpZ[index]*GmXpZ[index];

                   // Three connected pixels

                   pXpYpZ = index+rows+1+pagesize;
                   mXpYpZ = index+rows-1+pagesize;
                   pXmYpZ = index-rows+1+pagesize;
                   mXmYpZ = index-rows-1+pagesize;
                   

                   GpXpYpZ[index] = cnst2*MASK[pXpYpZ]*(In[pXpYpZ]-In[index]);
                   GmXpYpZ[index] = cnst2*MASK[mXpYpZ]*(In[mXpYpZ]-In[index]);
                   GpXmYpZ[index] = cnst2*MASK[pXmYpZ]*(In[pXmYpZ]-In[index]);
                   GmXmYpZ[index] = cnst2*MASK[mXmYpZ]*(In[mXmYpZ]-In[index]);
                   Gradient[index] += GpXpYpZ[index]*GpXpYpZ[index] + GmXpYpZ[index]*GmXpYpZ[index];
                   Gradient[index] += GpXmYpZ[index]*GpXmYpZ[index] + GmXmYpZ[index]*GmXmYpZ[index];
               }
              
           }

   for(ix=1;ix<dim[0]-1;ix++) 
	for(iy=1;iy<dim[1]-1;iy++) 
		for(iz=1;iz<dim[2]-1;iz++) 
		{
               // One connected pixels
			index   = ix + iy*rows + iz*pagesize;
               pX = index-1;
               pY = index-rows;
               pZ = index-pagesize;
               Laplacian[index]  = GpX[pX]-GpX[index];
               Laplacian[index] += GpY[pY]-GpY[index];
               Laplacian[index] += GpZ[pZ]-GpZ[index];
               
               // Two connected pixels
               pXpY = index-rows-1;
               pXmY = index+rows-1;
               pYpZ = index-pagesize-rows;
               mYpZ = index-pagesize+rows;
               pXpZ = index-pagesize-1;
               mXpZ = index-pagesize+1;
               
               Laplacian[index] += cnst1*(GpXpY[pXpY]-GpXpY[index]);
               Laplacian[index] += cnst1*(GpXmY[pXmY]-GpXmY[index]);
               Laplacian[index] += cnst1*(GpYpZ[pYpZ]-GpYpZ[index]);
               Laplacian[index] += cnst1*(GmYpZ[mYpZ]-GmYpZ[index]);
               Laplacian[index] += cnst1*(GpXpZ[pXpZ]-GpXpZ[index]);
               Laplacian[index] += cnst1*(GmXpZ[mXpZ]-GmXpZ[index]);
               
               // Three connected pixels

                   
               pXpYpZ = index-rows-1-pagesize;
               mXpYpZ = index-rows+1-pagesize;
               pXmYpZ = index+rows-1-pagesize;
               mXmYpZ = index+rows+1-pagesize;
                   
               Laplacian[index] += cnst2*(GpXpYpZ[pXpYpZ]-GpXpYpZ[index]);
               Laplacian[index] += cnst2*(GmXpYpZ[mXpYpZ]-GmXpYpZ[index]);
               Laplacian[index] += cnst2*(GpXmYpZ[pXmYpZ]-GpXmYpZ[index]);
               Laplacian[index] += cnst2*(GmXmYpZ[mXmYpZ]-GmXmYpZ[index]);
                
            }
   
   
}





void mexFunction(
int nlhs,              // Number of left hand side (output) arguments
mxArray *plhs[],       // Array of left hand side arguments
int nrhs,              // Number f right hand side (input) arguments
const mxArray *prhs[]  // Array of right hand side arguments
                    )
{
int *temp;
int dim[3];
double *Gradient, *Laplacian, *In, *MASK;
   double *GpX, *GpY, *GpZ;
double *GpXpY, *GpXmY, *GpYpZ,*GmYpZ, *GpXpZ, *GmXpZ;
double *GpXpYpZ, *GmXpYpZ, *GpXmYpZ,*GmXmYpZ;	

   
if (nrhs!= 2 || nlhs!=15)
   mexErrMsgTxt("Usage: [Gradient,Laplacian,GpX,GpY,GpZ,GpXpY,GpXmY,GpXmY,GmYpZ,GpXpZ,GmXpZ]=giveGradient1(In,Mask)");

   temp = mxGetDimensions(prhs[0]);
  /* printf("number of temp dimensions = %d n",temp); ssssssssss commented because I want speed
    printf("number of temp1 dimensions = %d n",temp[0]);
     printf("number of temp2 dimensions = %d n",temp[1]);
      printf("number of temp3 dimensions = %d n",temp[2]);*/
   dim[0] = temp[0];dim[1] = temp[1];dim[2] = temp[2];

plhs[0] = mxCreateNumericArray(3, dim, mxDOUBLE_CLASS, mxREAL);
Gradient  = mxGetPr(plhs[0]);
plhs[1] = mxCreateNumericArray(3, dim, mxDOUBLE_CLASS, mxREAL);
Laplacian  = mxGetPr(plhs[1]);

   plhs[2] = mxCreateNumericArray(3, dim, mxDOUBLE_CLASS, mxREAL);
GpX  = mxGetPr(plhs[2]);
plhs[3] = mxCreateNumericArray(3, dim, mxDOUBLE_CLASS, mxREAL);
GpY  = mxGetPr(plhs[3]);
plhs[4] = mxCreateNumericArray(3, dim, mxDOUBLE_CLASS, mxREAL);
GpZ  = mxGetPr(plhs[4]);

   plhs[5] = mxCreateNumericArray(3, dim, mxDOUBLE_CLASS, mxREAL);
GpXpY  = mxGetPr(plhs[5]);
plhs[6] = mxCreateNumericArray(3, dim, mxDOUBLE_CLASS, mxREAL);
GpXmY  = mxGetPr(plhs[6]);
plhs[7] = mxCreateNumericArray(3, dim, mxDOUBLE_CLASS, mxREAL);
GpYpZ  = mxGetPr(plhs[7]);
   plhs[8] = mxCreateNumericArray(3, dim, mxDOUBLE_CLASS, mxREAL);
GmYpZ  = mxGetPr(plhs[8]);
plhs[9] = mxCreateNumericArray(3, dim, mxDOUBLE_CLASS, mxREAL);
GpXpZ  = mxGetPr(plhs[9]);
plhs[10] = mxCreateNumericArray(3, dim, mxDOUBLE_CLASS, mxREAL);
GmXpZ  = mxGetPr(plhs[10]);
   
   plhs[11] = mxCreateNumericArray(3, dim, mxDOUBLE_CLASS, mxREAL);
GpXpYpZ  = mxGetPr(plhs[11]);
   plhs[12] = mxCreateNumericArray(3, dim, mxDOUBLE_CLASS, mxREAL);
GmXpYpZ  = mxGetPr(plhs[12]);
plhs[13] = mxCreateNumericArray(3, dim, mxDOUBLE_CLASS, mxREAL);
GpXmYpZ  = mxGetPr(plhs[13]);
plhs[14] = mxCreateNumericArray(3, dim, mxDOUBLE_CLASS, mxREAL);
GmXmYpZ  = mxGetPr(plhs[14]);

   In  = mxGetPr(prhs[0]);
MASK = mxGetPr(prhs[1]);

   giveGradient(GpX,GpY,GpZ,GpXpY,GpXmY,GpYpZ,GmYpZ,GpXpZ,GmXpZ,GpXpYpZ,GmXpYpZ,GpXmYpZ,GmXmYpZ,Gradient,Laplacian,In,MASK,dim);
  }			
	

 

And the errors are

>> mex givegradient.c
Warning: You are using gcc version "4.1.2".  The earliest gcc version supported
with mex is "3.4.0".  The latest version tested for use with mex is "3.4.5".
To download a different version of gcc, visit http://gcc.gnu.org 
givegradient.c:2: error: expected identifier or ‘(’ before ‘/’ token
givegradient.c:16: error: expected identifier or ‘(’ before ‘/’ token
givegradient.c:142: error: expected declaration specifiers or ‘...’ before ‘/’ token
givegradient.c:143: error: expected declaration specifiers or ‘...’ before ‘/’ token
givegradient.c:144: error: expected declaration specifiers or ‘...’ before ‘/’ token
givegradient.c:147: error: conflicting types for ‘mexFunction’
/usr/local/matlab/extern/include/mex.h:148: error: previous declaration of ‘mexFunction’ was here
givegradient.c: In function ‘mexFunction’:
givegradient.c:156: error: ‘nrhs’ undeclared (first use in this function)
givegradient.c:156: error: (Each undeclared identifier is reported only once
givegradient.c:156: error: for each function it appears in.)
givegradient.c:159: error: ‘prhs’ undeclared (first use in this function)
givegradient.c:159: warning: assignment discards qualifiers from pointer target type
givegradient.c:166: error: ‘plhs’ undeclared (first use in this function)

   mex: compile of 'givegradient.c' failed.

??? Error using ==> mex
Unable to complete successfully.

Link to comment
Share on other sites

Um, it might be that because the file is a ".c" file, that gcc thinks its ANSI C, which does not recognize "//" as a comment line (although Microsoft C++ might!).

 

I'd try changing the extension on the source file to ".cpp" or else edit your "//" lines to "/* blah */"....

 

I haven't touched gcc in years, so I may have no idea of what I'm talking about! :help:

 

Syntactically correct, :)

Buffy

Link to comment
Share on other sites

yeah, its because you used gcc, gcc by default thinks that any file passed to it as a C file, unless otherwise specified, that is why g++ was created, because unlike gcc, g++ thinks that any file passed to it is cpp by default.

 

you may want to link your mex to g++ vs gcc... you could make a make file too ;)

 

here is something that may be of help: Tutorial on MATLAB executables (MEX files)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...