Using the C++ Math Library | ![]() ![]() |
Stream I/O Format Definitions
The MATLAB C++ Math Library input and output formats strongly resemble their interpreted MATLAB counterparts. The array output format conforms to the rules for array input, which means that arrays written to a stream using <<
can be read in from a stream using >>
.
Note
The >> and << operator implementations do not read and write MAT-files. Use the functions load() and save() to read and write MAT-files. See the section Importing and Exporting MAT-File Data for more information. |
In the MATLAB C++ Math Library, special input characters describe the shape of the array. The [
and ]
characters (brackets) enclose an array definition. The { and } characters (braces) enclose a cell array definition. Within the brackets or braces, the contents of the array appear in row-major order. A semicolon (;
) separates rows.
The following table lists the syntax elements in the format definition.
Legal Array Elements
You can also specify a scaling factor that modifies the values in an mwArray
containing integers, floating-point numbers, or complex numbers. A scaling factor applies equally to all elements in the array and is used to enter very small or very large values.
Length of Input Array
The only restriction on the length of the input array is the amount of memory available; the input mechanism imposes no restrictions of its own.
How Whitespace Is Interpreted
Characteristics of Input Files
Input files must be in ASCII rather than binary format. An input file may contain multiple array definitions. Whitespace between array definitions is ignored.
Differences between MATLAB and the C++ Math Library
There are differences between the input accepted by MATLAB and the MATLAB C++ Math Library. MATLAB input files permit the use of MATLAB mathematical expressions in array definitions. The MATLAB C++ Math Library does not support the use of functions or operators in input streams.
For example, the MATLAB C++ Math Library does not support this:
[ (1 + 2) 7; 4 5]
Specifying an Array for Input
[
.
;
).
]
. However, do not end the last row with a semicolon.Input |
Array |
Array Type |
|
|
2-by-2 square array |
|
|
3-by-2 rectangular array |
Input |
Array |
Array Type |
|
|
2-by-2 complex square array |
Using a Data File As Input
Let an input data file, data
, contain the following array definition:
[ 1 2 3 ; 4 5 6 ]
Assume that a program called "io
" reads an array from standard input and then writes it to standard output.
Passing the file data
to the program io
io < data
[ 1 2 3 ; 4 5 6 ]
![]() | Example - Array Stream I/O (ex1.cpp) | Using Stream I/O to Files | ![]() |