2

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

cadolphs
  • 9,014
  • 1
  • 24
  • 41
smilingbuddha
  • 14,334
  • 33
  • 112
  • 189
  • 1
    possible duplicate of [How to call MATLAB code from C?](http://stackoverflow.com/questions/1513583/how-to-call-matlab-code-from-c) – gnovice Nov 27 '10 at 03:32

2 Answers2

1

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 )

Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
cadolphs
  • 9,014
  • 1
  • 24
  • 41
0

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.

stijn
  • 34,664
  • 13
  • 111
  • 163