Matio User Guide
Matio User Guide
User Manual
for version 1.5.27
April 2024
Christopher C. Hulbert
Copyright (c) 2015-2024, The matio contributors.
Copyright (c) 2011-2014, Christopher C. Hulbert.
All rights reserved.
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
i
Table of Contents
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 About and Licensing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Incompatible Changes from 1.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2.1 Type Change for Dimensions Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2.2 Removed Preprocessor Flag to Conserve Memory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2.3 Renamed Structure Field Lookup Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2.4 Memory Conservation with Cells and Structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2 Quick Start. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.1 Opening and Creating MAT Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.2 Reading Variables in a MAT File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2.1 Reading a Variable by Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2.2 Iterating Over Variables in a MAT File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.3 Writing Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.3.1 Writing Numeric Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.3.2 Writing Cell Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3.3 Writing Structure Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3 Building matio. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.1 Quick Build Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.2 Configure Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.3 CMake build system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.4 Visual Studio. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.5 Testsuite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1 Introduction
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
2 Quick Start
int
main(int argc,char **argv)
{
mat_t *matfp;
matfp = Mat_Open(argv[1],MAT_ACC_RDONLY);
if ( NULL == matfp ) {
fprintf(stderr,"Error opening MAT file \"%s\"!\n",argv[1]);
return EXIT_FAILURE;
}
Mat_Close(matfp);
return EXIT_SUCCESS;
}
The Mat_CreateVer creates a new MAT file (or overwrites an existing file) with a specific
version. The matio library can write version 4 MAT files, version 5 MAT files, version 5 MAT
files with variable compression (if built with zlib), and HDF5 format MAT files introduced in
MATLAB version 7.3. The format of the MAT file is specified by the third argument. The short
example below creates a version 4 MAT file named matfile4.mat, a version 5 MAT file named
matfile5.mat and an HDF5 format MAT file named matfile73.mat.
#include <stdlib.h>
#include <stdio.h>
#include "matio.h"
int
main(int argc,char **argv)
{
mat_t *matfp;
matfp = Mat_CreateVer("matfile4.mat",NULL,MAT_FT_MAT4);
if ( NULL == matfp ) {
fprintf(stderr,"Error creating MAT file \"matfile4.mat\"!\n");
return EXIT_FAILURE;
4 MATIO
}
Mat_Close(matfp);
matfp = Mat_CreateVer("matfile5.mat",NULL,MAT_FT_MAT5);
if ( NULL == matfp ) {
fprintf(stderr,"Error creating MAT file \"matfile5.mat\"!\n");
return EXIT_FAILURE;
}
Mat_Close(matfp);
matfp = Mat_CreateVer("matfile73.mat",NULL,MAT_FT_MAT73);
if ( NULL == matfp ) {
fprintf(stderr,"Error creating MAT file \"matfile73.mat\"!\n");
return EXIT_FAILURE;
}
Mat_Close(matfp);
return EXIT_SUCCESS;
}
int
main(int argc,char **argv)
{
mat_t *matfp;
matvar_t *matvar;
matfp = Mat_Open(argv[1],MAT_ACC_RDONLY);
if ( NULL == matfp ) {
fprintf(stderr,"Error opening MAT file \"%s\"!\n",argv[1]);
return EXIT_FAILURE;
}
Chapter 2: Quick Start 5
matvar = Mat_VarReadInfo(matfp,"x");
if ( NULL == matvar ) {
fprintf(stderr,"Variable ’x’ not found, or error "
"reading MAT file\n");
} else {
if ( !matvar->isComplex )
fprintf(stderr,"Variable ’x’ is not complex!\n");
if ( matvar->rank != 2 ||
(matvar->dims[0] > 1 && matvar->dims[1] > 1) )
fprintf(stderr,"Variable ’x’ is not a vector!\n");
Mat_VarFree(matvar);
}
Mat_Close(matfp);
return EXIT_SUCCESS;
}
#include <stdlib.h>
#include <stdio.h>
#include "matio.h"
int
main(int argc,char **argv)
{
mat_t *matfp;
matvar_t *matvar;
matfp = Mat_Open(argv[1],MAT_ACC_RDONLY);
if ( NULL == matfp ) {
fprintf(stderr,"Error opening MAT file \"%s\"!\n",argv[1]);
return EXIT_FAILURE;
}
Mat_Close(matfp);
return EXIT_SUCCESS;
}
6 MATIO
int
main(int argc,char **argv)
{
mat_t *matfp;
matvar_t *matvar;
size_t dims[2] = {10,1};
double x[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9,10},
y[10] = {11,12,13,14,15,16,17,18,19,20};
struct mat_complex_split_t z = {x,y};
matfp = Mat_CreateVer("test.mat",NULL,MAT_FT_DEFAULT);
if ( NULL == matfp ) {
fprintf(stderr,"Error creating MAT file \"test.mat\"\n");
return EXIT_FAILURE;
}
matvar = Mat_VarCreate("x",MAT_C_DOUBLE,MAT_T_DOUBLE,2,dims,x,0);
if ( NULL == matvar ) {
fprintf(stderr,"Error creating variable for ’x’\n");
} else {
Mat_VarWrite(matfp,matvar,MAT_COMPRESSION_NONE);
Mat_VarFree(matvar);
}
matvar = Mat_VarCreate("y",MAT_C_DOUBLE,MAT_T_DOUBLE,2,dims,y,0);
if ( NULL == matvar ) {
Chapter 2: Quick Start 7
matvar = Mat_VarCreate("z",MAT_C_DOUBLE,MAT_T_DOUBLE,2,dims,&z,
MAT_F_COMPLEX);
if ( NULL == matvar ) {
fprintf(stderr,"Error creating variable for ’z’\n");
} else {
Mat_VarWrite(matfp,matvar,MAT_COMPRESSION_NONE);
Mat_VarFree(matvar);
}
Mat_Close(matfp);
return EXIT_SUCCESS;
}
int
main(int argc,char **argv)
{
mat_t *matfp;
matvar_t *cell_array, *cell_element;
size_t dims[2] = {10,1};
double x[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9,10},
y[10] = {11,12,13,14,15,16,17,18,19,20};
struct mat_complex_split_t z = {x,y};
matfp = Mat_CreateVer("test.mat",NULL,MAT_FT_DEFAULT);
if ( NULL == matfp ) {
fprintf(stderr,"Error creating MAT file \"test.mat\"\n");
return EXIT_FAILURE;
}
dims[0] = 3;
dims[1] = 1;
cell_array = Mat_VarCreate("a",MAT_C_CELL,MAT_T_CELL,2,dims,NULL,0);
if ( NULL == cell_array ) {
fprintf(stderr,"Error creating variable for ’a’\n");
Mat_Close(matfp);
return EXIT_FAILURE;
8 MATIO
dims[0] = 10;
dims[1] = 1;
cell_element = Mat_VarCreate(NULL,MAT_C_DOUBLE,MAT_T_DOUBLE,2,dims,x,0);
if ( NULL == cell_element ) {
fprintf(stderr,"Error creating cell element variable\n");
Mat_VarFree(cell_array);
Mat_Close(matfp);
return EXIT_FAILURE;
}
Mat_VarSetCell(cell_array,0,cell_element);
cell_element = Mat_VarCreate(NULL,MAT_C_DOUBLE,MAT_T_DOUBLE,2,dims,y,0);
if ( NULL == cell_element ) {
fprintf(stderr,"Error creating cell element variable\n");
Mat_VarFree(cell_array);
Mat_Close(matfp);
return EXIT_FAILURE;
}
Mat_VarSetCell(cell_array,1,cell_element);
cell_element = Mat_VarCreate(NULL,MAT_C_DOUBLE,MAT_T_DOUBLE,2,dims,&z,
MAT_F_COMPLEX);
if ( NULL == cell_element ) {
fprintf(stderr,"Error creating cell element variable\n");
Mat_VarFree(cell_array);
Mat_Close(matfp);
return EXIT_FAILURE;
}
Mat_VarSetCell(cell_array,2,cell_element);
Mat_VarWrite(matfp,cell_array,MAT_COMPRESSION_NONE);
Mat_VarFree(cell_array);
Mat_Close(matfp);
return EXIT_SUCCESS;
}
int
main(int argc,char **argv)
{
mat_t *matfp;
matvar_t *matvar, *field;
size_t dims[2] = {10,1}, struct_dims[2] = {2,1};
double x1[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9,10},
x2[10] = {11,12,13,14,15,16,17,18,19,20},
y1[10] = {21,22,23,24,25,26,27,28,29,30},
y2[10] = {31,32,33,34,35,36,37,38,39,40};
struct mat_complex_split_t z1 = {x1,y1}, z2 = {x2,y2};
const char *fieldnames[3] = {"x","y","z"};
unsigned nfields = 3;
matfp = Mat_CreateVer("test.mat",NULL,MAT_FT_DEFAULT);
if ( NULL == matfp ) {
fprintf(stderr,"Error creating MAT file \"test.mat\"\n");
return EXIT_FAILURE;
}
/* structure index 0 */
field = Mat_VarCreate(NULL,MAT_C_DOUBLE,MAT_T_DOUBLE,2,dims,x1,0);
Mat_VarSetStructFieldByName(matvar,"x",0,field);
field = Mat_VarCreate(NULL,MAT_C_DOUBLE,MAT_T_DOUBLE,2,dims,y1,0);
Mat_VarSetStructFieldByName(matvar,"y",0,field);
field = Mat_VarCreate(NULL,MAT_C_DOUBLE,MAT_T_DOUBLE,2,dims,&z1,
MAT_F_COMPLEX);
Mat_VarSetStructFieldByName(matvar,"z",0,field);
/* structure index 1 */
field = Mat_VarCreate(NULL,MAT_C_DOUBLE,MAT_T_DOUBLE,2,dims,x2,0);
Mat_VarSetStructFieldByName(matvar,"x",1,field);
field = Mat_VarCreate(NULL,MAT_C_DOUBLE,MAT_T_DOUBLE,2,dims,y2,0);
Mat_VarSetStructFieldByName(matvar,"y",1,field);
field = Mat_VarCreate(NULL,MAT_C_DOUBLE,MAT_T_DOUBLE,2,dims,&z2,
MAT_F_COMPLEX);
Mat_VarSetStructFieldByName(matvar,"z",1,field);
Mat_VarWrite(matfp,matvar,MAT_COMPRESSION_NONE);
Mat_VarFree(matvar);
Mat_Close(matfp);
return EXIT_SUCCESS;
}
Chapter 3: Building matio 11
3 Building matio
$ cmake --build .
$ cmake --install .
The following matio specific options for building with CMake are available.
MATIO_USE_CONAN:BOOL=OFF
This option enables the Conan package manager to resolve the library dependencies.
MATIO_DEFAULT_FILE_VERSION:STRING=5
This option sets the default MAT file version (4,5,7.3) that will be used when writing.
MATIO_EXTENDED_SPARSE:BOOL=ON
This option enables extended sparse matrix data types not supported in MATLAB.
MATIO_MAT73:BOOL=ON
This flag enables the support for version 7.3 MAT files.
MATIO_PIC:BOOL=ON
This option enables position-independent code (PIC), i.e., compilation with the -
fPIC flag. It is ignored for Visual Studio builds.
MATIO_SHARED:BOOL=ON
This option builds the matio library as shared object (i.e., a dynamic link library
on Windows).
MATIO_WITH_HDF5:BOOL=ON
This option enables CMake to check for availability of the HDF5 library (see section
2.1.2 for information about HDF5).
MATIO_WITH_ZLIB:BOOL=ON
This option enables CMake to check for availability of the zlib library (see section
2.1.1 for information about zlib).
To help CMake find the HDF5 libraries, set environment variable HDF5 DIR to the
cmake/hdf5 directory (containing hdf5-config.cmake) inside the HDF5 build or installation di-
rectory, or call cmake with -DHDF5_DIR="dir/to/hdf5/cmake/hdf5. Alternatively call CMake
with -DCMAKE_PREFIX_PATH="dir/to/hdf5/cmake". See the HDF5 instructions for more in-
formation. Using hdf5-config.cmake is recommended over using CMake’s built-in FindHDF5,
especially for static builds. CMake 3.10 or later is recommended.
3.5 Testsuite
A testsuite is available when building with the GNU autotools. To run the testsuite, First
configure and build matio. After building run make check to run the testsuite. If matio was
built without zlib, the compressed variable tests will be skipped. If built without HDF5, the
tests for version 7.3 MAT files will be skipped. If the path to the MATLAB application was
not specified (--with-matlab), the write tests will fail if matio cannot read the file and skip if
matio can read the file. The write tests will pass if MATLAB is available and can also read the
file.
To report matio testsuite failures, compress the testsuite.log file in the test sub-directory of
the build directory. Upload the compressed log file along with a bug report (see Section 1.4 for
information on reporting bugs).
Chapter 4: MATLAB Variable Structure 13