Using the C++ Math Library    

Example Program: Creating Arrays and Array I/O (ex1.cpp)

This example demonstrates how to create an array from static data. Its primary purpose is to present a simple yet complete program. The code creates two matrices, prints them, and then reads in and prints out a third matrix. Each of the numbered sections of code is explained in more detail below.

You can find the code for this example in the
<matlab>/extern/examples/cppmath directory on UNIX systems and in the <matlab>\extern\examples\cppmath directory on PCs, where <matlab> represents the top-level directory of your installation. See Building C++ Applications for information about building and running the example program.

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

  1. Include header files. matlab.hpp declares the MATLAB C++ Math Library's data types and functions. matlab.hpp includes iostream.h, which declares the input and output streams cin and cout.  stdlib.h contains the definition of EXIT_SUCCESS.
  2. Declare a static array of real numbers for later use as input to an mwArray constructor. Note that the C++ array is one-dimensional, even though it is used to create two-dimensional matrices. Because MATLAB stores its arrays in column-major order, data that initializes a MATLAB C++ Math Library array must also be in column-major order. C++ itself, however, stores arrays in row-major order.

    To arrange mwArray data in column-major order, read down the columns of an array from the leftmost column to the rightmost column. To avoid confusion, always use one-dimensional C++ arrays to initialize mwArray objects.

  1. Create two matrices by using mwArray constructors, which declare and initialize the variables, mat0 and mat1. The first matrix, mat0, has two rows and three columns; the second matrix, mat1, has three rows and two columns. Each call to the constructor takes the static array data as an argument. Constructors copy data.
  2. Print the matrices using the C++ standard output stream, cout. By default, objects printed with cout appear on the terminal screen, though you can redirect the output to a file. A stream is a sequence of bytes that can be read from or written to. It is more general than a file, encompassing all the I/O devices attached to a computer (keyboard, terminal screen, disk, etc.). Refer to your C++ reference for a complete explanation of streams and C++'s input and output facilities.
  3. Prompt the user to type in a matrix. Read the matrix into mat1 using the C++ standard input stream, cin. The matrix does not need to be the same size as the matrix already stored in mat1. The input operator >> creates a new matrix and assigns that matrix to mat1. UNIX systems and PCs read from the terminal by default; you can redirect them to read from an input file. See Using Array Stream I/O in Chapter 8 to learn about the I/O format for the library and how it differs from MATLAB's.
  4. Print the newly read matrix.

Output

The program prints the matrices, mat0 and mat1, and then prompts the user to enter a matrix.

If you enter a valid matrix, for example, [ 1 0; 0 1], the program prints it.

Note that the output format is the same as the input format, enabling the output from one program to be used as the input to another. The input format is simple. Matrix text begins with the character [. The opening bracket is followed by any number of rows of integers or floating-point numbers separated by semicolons. Each row must contain the same number of columns. Matrix text ends with a ]. Spaces, tabs, and carriage returns are ignored. For complete information about using stream I/O, see Using Array Stream I/O.


 Initializing a Numeric Array with Data Sparse Matrices