Using the C Math Library    

Example - Calling Library Routines (ex3.c)

This example program illustrates how to call library routines that take multiple, optional arguments. The example uses the singular value decomposition function mlfSvd.

You can find the code for this example in the
<matlab>/extern/examples/cmath directory on UNIX systems or the <matlab>\extern\examples\cmath directory on PCs, where <matlab> represents the top-level directory of your installation. See Building Stand-Alone C Applications in Chapter 2 for information on building the examples.

The numbered items in the list below correspond to the numbered comments in the code example:

  1. Include "matlab.h". This file contains the declaration of the mxArray data structure and the prototypes for all the functions in the library. stdlib.h contains the definition of EXIT_SUCCESS.
  2. Declare the eight-element static array that subsequently initializes the mlfSvd input matrix. The elements in this array appear in column-major order. The MATLAB C Math Library stores its array data in column-major order, unlike C, which stores array data in row-major order.
  3. Declare and initialize the mlfSvd input array, X. Declare and initialize mxArray* variables, U, S, and V, to be used as output arguments in later calls to mlfSvd.
  4. mlfSvd can be called in three different ways. Call it the first way, with one input matrix and one output matrix. Note that the optional inputs and outputs in the parameter list are set to NULL. Optional, in this case, does not mean that the arguments can be omitted from the parameter list; instead it means that the argument is optional to the workings of the function and that it can be set to NULL.

    Print the result of the call to mlfSvd().

    If you want to know more about the function mlfSvd() or the calling conventions for the library, refer to the online MATLAB C Math Library Reference.

  1. Call mlfSvd the second way, with three output arguments and one input argument. The additional output arguments, S and V, appear first in the argument list. Because the return value from mlfSvd corresponds to the first output argument, U, only two output arguments, S and V, appear in the argument list, bringing the total number of outputs to three. The next argument, X, is the required input argument. Only the final argument, the optional input, is passed as NULL.

    Print all of the output matrices.

  1. Call mlfSvd the third way, with three output arguments and two input arguments. Print all of the output matrices.

    Notice that in this call, as in the previous one, an ampersand (&) precedes the two additional output arguments. An ampersand always precedes each output argument because the address of the mxArray* is passed. The presence of an & is a reliable way to distinguish between input and output arguments. Input arguments never have an & in front of them.

  1. Last of all, free all of the matrices that have been bound to variables.

    Output

When the program is run, it produces this output.


 Summary of Library Calling Conventions Calling Operators