smilingbuddha
smilingbuddha

Reputation: 14670

Interfacing MATLAB with C/C++ programs

Hi I wanted to know how to use MATLAB as an external solver from a C program. Specifically in my code I wish to solve several linear systems of the form Ax=b.

I have heard that to go the other way namely Calling C functions in a MATLAB routine one uses MEX files.But I am not really sure how to use Mex files either.

Thank you

Upvotes: 2

Views: 806

Answers (2)

cadolphs
cadolphs

Reputation: 9657

Actually, MEX files allow you to include C code in Matlab programs, for example if you want to use external C libraries in Matlab.

What you want to do is use the Matlab Engine: http://www.mathworks.com/help/techdoc/matlab_external/f29148.html

As an alternative, you could use linear algebra libraries that are written purely in C, such as LAPACK and BLAS. ( www.netlib.org )

Upvotes: 1

stijn
stijn

Reputation: 35911

you can use the matlab engine as Lagerbaer points out. However sometimes it can be convenient just calling the matlab process commandline style. I use this often when I don't want to mess with mxArrays etc, or when the amount of matlab code that needs executing gets really large. PseudoCode:

WriteArrayInMFormat( "in.m", myInputNumbers );
LaunchProcess( "matlab", "-nodesktop -r \"myFunction( 'in.m' )\" -logfile out.m" );
ReadArrayInMFormat( "out.m", myResult );

For me this is especially useful when testing things out: instead of having to recompile the C/C++ program each time I change something, I just apply all changes in the myFunction.m file. At that point I don't even need the C program, instead everything can be tested in matlab.

Upvotes: 0

Related Questions