views:

405

answers:

3

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?

+1  A: 

The generic answer is to use system().

Everything else is very OS-dependent.

Dirk Eddelbuettel
+12  A: 

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

Pete Kirkham
+2  A: 

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.

Sam Roberts
I linked to the wrong product - generating C code from Matlab is fairly fundamental to how it's used in many industries.
Pete Kirkham
Yes - if you want to generate C code from MATLAB for embedded applications, you can do that using the emlc command. This requires that you also have both Simulink and Real-Time Workshop, though (not MATLAB Compiler), and that seems like overkill in order to plot points of a triangle :)
Sam Roberts