# mexCallMATLAB

## Purpose

Call internal MATLAB functions.

## C Synopsis

    #include "mex.h"
    int mexCallMATLAB(nlhs,plhs,nrhs,prhs,name)
      int nlhs;
      Matrix *plhs[];
      int nrhs;
      Matrix *prhs[];
      char *name;

## Fortran Synopsis

    integer*4 function mexCallMATLAB(nlhs,plhs,nrhs,prhs,name)
    integer*4 nlhs,nrhs,plhs(*),prhs(*)
    character*(*) name

## Arguments

`name`
  character string with function name
`nlhs`
  number of desired outputs
`nrhs`
  number of inputs
`plhs`
  pointer array for outputs
`prhs`
  pointer array for inputs

## Description

This routine allows your MEX-file to invoke internal MATLAB numeric functions, M-files, or other MEX-files. See `mexFunction` for a complete description of the arguments.

MATLAB operators (such as Matrix multiplication, division, addition, etc.) are referenced by strings containing the operator symbols`` *`/ +`.

This routine allocates new Matrix structures each time it is called, so it is important to return them using `mxFreeMatrix` when necessary.

If an error is encountered within the call to MATLAB, the default response is for this routine to never return and for the program to longjmp out of the MEX-file, returning control directly to the MATLAB prompt. The routine `mexSetTrapFlag` can be used to disable this response to errors. In this case, `mexCallMATLAB` returns zero if the operation is successful and a nonzero value if an error is encountered.

## Examples

Here is a code fragment which illustrates how to use `mexCallMATLAB`. The example generates and prints a Matrix, computes its eigenvalues and eigenvectors, and prints out the eigenvalue Matrix.

    /* mextest1.c */
    int m, n, mn, mrhs, mlhs;
    Matrix *lhs[2], *x;
    m = n = 4;
    x =  mxCreateFull(m,n,IMAG);
    /* Fill matrix with data and print it out */
    fill(mexGetPr(x), mexGetPi(x));
    mrhs = 1; mlhs = 0;
    mexCallMATLAB(mlhs,lhs,mrhs,&x,"disp");
    /* calculate eigenvalues and eigenvectors */
    mlhs = 2;
    mexCallMATLAB(mlhs,lhs,mrhs,&x,"eig");
    /* print out eigenvalue matrix */
    mlhs = 0;
    mexCallMATLAB(mlhs,lhs,mrhs,&lhs[1],"disp");
    /* Free allocated matrices */
    mxFreeMatrix(x);
    mxFreeMatrix(lhs[1]);
    mxFreeMatrix(lhs[0]);

A Fortran version of this is in `mextest1.f`.

(c) Copyright 1994 by The MathWorks, Inc.