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.
// ex1.cpp
#include <stdlib.h>
#include "matlab.hpp" // See Note 1
static double data[] = { 1, 2, 3, 4, 5, 6 }; // See Note 2
int main(void)
{
// Create two matrices.
mwArray mat0(2, 3, data); // See Note 3
mwArray mat1(3, 2, data);
// Print the matrices.
cout << mat0 << endl; // See Note 4
cout << mat1 << endl;
// Read a matrix from standard in, then print the matrix to
// standard out.
cout << "Please enter a matrix: " << endl; // See Note 5
cin >> mat1;
cout << mat1 << endl; // See Note 6
return (EXIT_SUCCESS);
}
The numbered items in the list below correspond to the numbered comments in the code example:
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
.
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.
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.
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.
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.
Output
The program prints the matrices, mat0
and mat1
, and then prompts the user to enter a matrix.
[ 1 3 5 ; 2 4 6 ] [ 1 4 ; 2 5 ; 3 6 ] Please enter a matrix:
If you enter a valid matrix, for example, [ 1 0; 0 1]
, the program prints it.
[ 1 0 ; 0 1 ]
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.
Note Because the array input format is the same as the array output format, data written out by one program can be easily read in by another program. |
![]() | Initializing a Numeric Array with Data | Sparse Matrices | ![]() |