Calling MATLAB from C
I'm writing a Java application that needs to be able to run MATLAB commands. To do so, I'm using a C program that the Java application can call upon to interface with MATLAB. However, even after researching the MATLAB engine, I can't seem to figure 开发者_C百科out how to compile the C program. This documentation seems to be compiling the C program from within MATLAB: http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/f39903.html. Is there any way to compile from the command line? That is, can I use gcc with some flags to include all the relevant MATLAB support (I don't ordinarily program in C, so sorry if my language is not exactly correct!)
Thanks!
You can compile your code normally as you would do for a normal C program, but you need to specify the dependencies correctly.
The best way to do is to use the CMake utility with a CMakelists.txt file. Now, a demo on how to write the CMakeLists.txt is given here and how to interface C/C++ with Matlab is given here. I hope it helps....
Maybe you want do the following :
1) Compile your matlab code (i.e m files ) with the matlab compiler mcc from the matlab command line. mcc compiler generates c dll . The matlab generated c dll contains the c interface for the matlab m files. See the following link about how to generate c dll from matlab M files. C Shared Library Target
2) Write c dll that uses the generated matlab dll in step 1. Compile the c dll with c compiler.See the following link about how to call the functions that are inside the matlab generated dll MATLAB Compiler Generated Interface Functions
3) Use the generated c dll in step 2 with java.
I don't use MATLAB, but I'm guessing that you have to do something like this:
- Compile your C program with MATLAB libraries to create a shared library that Java can use.
- Write a JNI interface that calls your shared library, being sure to link in your new SO and all those from MATLAB that you need.
Break the problem into steps and you'll sort it out.
精彩评论