How to call MATLAB code from C?
Asked Answered
G

3

12

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?

Gneiss answered 3/10, 2009 at 12:8 Comment(0)
C
17

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

Chandelle answered 3/10, 2009 at 12:32 Comment(0)
S
10

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.

Sikko answered 5/10, 2009 at 15:15 Comment(3)
I linked to the wrong product - generating C code from Matlab is fairly fundamental to how it's used in many industries.Chandelle
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 :)Sikko
update: MathWorks now have a new product: MATLAB CoderMafaldamafeking
I
0

Look at this presentation about integrating MATLAB Algorithms in C or C++ Applications http://www.mathworks.com/videos/integrating-matlab-algorithms-in-c-or-c-applications-86431.html

Immense answered 24/11, 2013 at 20:9 Comment(1)
Hi! Answers, if referencing a link, should contain essential parts of a link, as links can break and no longer be accessable.Testamentary

© 2022 - 2024 — McMap. All rights reserved.