Science Forums
User Name
Password
Science Social Network
home    members    help/rules    who is online    contact   

Go Back   Science Forums > Physical Sciences Forums > Computer Science
Become a science forums sponsor today
Reply
 
LinkBack Thread Tools
Old 02-21-2008   #1 (permalink)
sanctus's Avatar
Resident Diabolist

Hypography Staff Member
Administrator

 



Using c code in Matlab...on xubuntu

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
Code:
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


----------------
Administrator

A COUNTRY WITHOUT AN ARMY IS LIKE A FISH WITHOUT A BIKE!!!


I don't believe in god, but I do believe in what others call utopies.

Last edited by sanctus; 02-21-2008 at 12:48 PM.
Reply With Quote
Old 02-21-2008   #2 (permalink)
alexander's Avatar
Resident USSRian

Hypography Staff Member
Administrator
Gallery Curator
Dev Team Member

Latest blog entry:
Open-Source HIDS
 
alexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant future
Send a message via AIM to alexander
 



Re: Using c code in Matlab...on xubuntu

need to see your code lol


----------------
And remember that great question that Pierre-Simon Laplace and Sir Isaac Newton, Andrei Markov and David Hilbert, Richard Feynman and Enrico Fermi, Albert Einstein and Edmund Halley did not come to ask throughout all of their dedication and work: "Who the hell is IMing me?"


This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 License.
Reply With Quote
Old 02-21-2008   #3 (permalink)
sanctus's Avatar
Resident Diabolist

Hypography Staff Member
Administrator

 



Re: Using c code in Matlab...on xubuntu

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

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,B) ((A)>(B)?(A):(B))
#define MIN(A,B) ((A)<(B)?(A):(B))
#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
Code:
>> 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.


----------------
Administrator

A COUNTRY WITHOUT AN ARMY IS LIKE A FISH WITHOUT A BIKE!!!


I don't believe in god, but I do believe in what others call utopies.
Reply With Quote
Old 02-21-2008   #4 (permalink)
Buffy's Avatar
Resident Slayer

Hypography Staff Member
Administrator

 



Re: Using c code in Matlab...on xubuntu

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!

Syntactically correct,
Buffy


----------------
"If you do not agree with anything I say, I'll not only retract it, but deny under oath that I ever said it!"
__________________________________________________ ______________-- Tom Lehrer

"The shrinks diagnosed me a sociopath with paranoid delusions. But they’re just out to get me cause I threatened to kill them."


Forum Administrator
Hypography Science Forums - Science for Boys and Girls! Its not for nothing that we hang out here.
Reply With Quote
Old 02-22-2008   #5 (permalink)
sanctus's Avatar
Resident Diabolist

Hypography Staff Member
Administrator

 



Re: Using c code in Matlab...on xubuntu

Thanks Buffy, it works fine with *.cc or *.cpp. This helped me a lot!


----------------
Administrator

A COUNTRY WITHOUT AN ARMY IS LIKE A FISH WITHOUT A BIKE!!!


I don't believe in god, but I do believe in what others call utopies.
Reply With Quote
Old 02-22-2008   #6 (permalink)
alexander's Avatar
Resident USSRian

Hypography Staff Member
Administrator
Gallery Curator
Dev Team Member

Latest blog entry:
Open-Source HIDS
 
alexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant futurealexander has a brilliant future
Send a message via AIM to alexander
 



Re: Using c code in Matlab...on xubuntu

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)


----------------
And remember that great question that Pierre-Simon Laplace and Sir Isaac Newton, Andrei Markov and David Hilbert, Richard Feynman and Enrico Fermi, Albert Einstein and Edmund Halley did not come to ask throughout all of their dedication and work: "Who the hell is IMing me?"


This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 License.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
New code discovered in DNA C1ay News in Brief 1 07-25-2006 03:44 PM
Break the code! ronthepon Watercooler 25 07-21-2006 02:12 AM
URL code tag test Turtle Test forum 7 07-15-2006 02:21 PM
Standard map in matlab sanctus Computer Science 5 05-03-2005 06:03 PM
unbreakable code rileyj Physics and Mathematics 0 04-10-2004 11:46 AM


All times are GMT -8. The time now is 03:55 PM.

Hypography?

Hypography [n.]: A combination of "hyperlink" and "bibliography" - ie, a list of links to electronic documents. Comparable to discography and bibliography, but not cartography.

We have been online since May 2000, and aim to be the best place to find and share science-related content of all kinds.

Share the love!

Please add more science to your life. Use our RSS feeds on your blog, your portal, or your favorite feedreader!

Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 ©2008, Crawlability, Inc. Copyright © 2000-2008 Hypography
Part of the Hypography - Science for Everyone Network