MEX and OpenGL

8 views (last 30 days)
Matt
Matt on 24 Jun 2011
Commented: Eric on 6 Oct 2014
I am developing a program in C that simulates species competing in an environment. I am using OpenGL to visualize the dynamics over time. The interactions between the species are dictated by a matrix of competitive outcomes (after picking two species to compete, I refer to this matrix to determine who wins). I want to have MATLAB process the various matrix indices I am interested in (for example, the Fiedler value), so this brings me into dealing with MEX (I think).
I will only be calling MATLAB functions from C (not vice-versa). Is creating a MEX-file required for this? If so, how do I compile the C code (on various platforms) to handle both the MEX linking and the OpenGL linking?
I mostly work on my MacBook Pro (running Mac OS X), but I am also interested in being able to compile this program in Windows and linux. On my Mac, when I am not including function calls to MATLAB I compile the program with "LDFLAGS = -lpthread -framework Carbon -framework OpenGL -framework GLUT", but when I add such calls into the code, try to compile with mex, and have those flags included, it complains about "-framework" not being a valid option.
The ultimate goal is to have the simulation/visualization happily running while MATLAB works on the competition matrix indices on the side.

Answers (1)

Chirag Gupta
Chirag Gupta on 24 Jun 2011
You don't need to create MEX files since you will be running your application in C.
You will need to make use of the MATLAB Engine Interface that allows you to send data from you C program, call MATLAB functions and return the answers back to C.
  2 Comments
Matt
Matt on 24 Jun 2011
Ok, good to know about not needing to deal with MEX.
I wasn't seeing anything about compiling with just gcc in either of those pages, so after searching around a little, I came across a blog entry discussing how to compile with gcc ( http://www.alecjacobson.com/weblog/?p=924 ). I have added
export MATLAB="/Applications/MATLAB_R2009aSV.app"
export DYLD_LIBRARY_PATH=$MATLAB/bin/maci:$MATLAB/sys/os/maci:$DYLD_LIBRARY_PATH
to my profile, and I try to compile with
gcc -o sim sim.c -I/Applications/MATLAB_R2009aSV.app/extern/include/ -L/Applications/MATLAB_R2009aSV.app/bin/maci/ -lpthread -leng -lm -lmat -lmx -lut -framework Carbon -framework OpenGL -framework GLUT
but it comes back with the error,
In file included from /Applications/MATLAB_R2009aSV.app/extern/include/engine.h:42,
from sim.c:2:
/Applications/MATLAB_R2009aSV.app/extern/include/matrix.h:722: error: expected ‘;’, ‘,’ or ‘)’ before numeric constant
At this point, I don't know what is going wrong or why it seems that matrix.h has an error. Any tips?
Eric
Eric on 6 Oct 2014
Have there been any developments on this issue?
I am trying to build a C program that interfaces with Matlab via Matlab Engine. This C program will also be utilizing OpenGL with glut. I have been trying to compile on Mac OSX 10.9.
I am having trouble getting both Matlab Engine and OpenGL libraries to be recognized by the compiler (e.g. glut.h, engine.h, and matrix.h). When I compile with mex, I can get simple Matlab Engine programs to compile, but it does not recognize my OpenGL headers. (It complains that the glut functions are undefined symbols.) When I try to compile with gcc, it recognizes the OpenGL, but not the Matlab Engine headers.

Sign in to comment.

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!