How to call MATLAB code from C?

I have some code that plots triangles in MATLAB.

I need to be able to somehow execute this code from my C program which generates these points.

Is that possible? How can it be done?

Just a thought: Can I somehow embed MATLAB code in C, so that it can compile on a C compiler?


The Mathworks site has full details; a demo video of calling the Matlab engine from C, and also the Matlab to C Compiler.


As mentioned previously by answerers, you can call a live copy of MATLAB from C via the MATLAB Engine interface.

If the end-product needs to be used where there is no live copy of MATLAB, you can deploy the application using MATLAB Compiler. However, MATLAB Compiler does not, as another answer has suggested, convert MATLAB programs into C code (and hasn't done for a few versions now). Instead, it archives and encrypts your MATLAB program, and packages it into an executable or shared library that executes against the MATLAB Compiler Runtime (shareable royalty-free). The executable or shared library can then be called from C.

Alternatively you could go the other way around, and call your C code from MATLAB, using either loadlibrary or MATLAB's MEX interface.

Update: As of release R2011a, you can also use MATLAB Coder to generate C code directly from a subset of the MATLAB language.