MatFile

 

class MatFile

 

MatFile is an RAII wrapper that opens, creates, reads, and writes MATLAB .mat files using the MatIO library. It supports MAT4, MAT5, and MAT7.3 formats, and it provides convenience methods for writing scalar values, strings, matrices (double, int, and complex), U++ containers, and Eigen matrices. It also includes static reading helpers for extracting data in row-major buffers and Eigen-friendly forms, plus a nested StructNode for building and writing structured variables.

 

 

Public Member List


 

bool OpenRead(const char *path)

Opens a .mat file read-only; returns true if the file handle is valid.

 


 

bool OpenReadWrite(const char *path)

Opens a .mat file for read and write; returns true if the file handle is valid.

 


 

bool OpenCreate(const char *path, mat_ft ver = enum mat_ft::MAT_FT_MAT5)

Creates a new .mat file for the specified format version; returns true if the file handle is valid.

 


 

mat_ft GetVersion()

Returns the format version of the open file; this method throws if the file is not open.

 


 

String GetVersionName()

Returns a human-readable version string ("4", "5", "7.3", or "unknown").

 


 

Vector<String> GetVarList() const

Returns the list of top-level variable names in the open file.

 


 

String GetVarName(const char *name) const

Returns the real name of a variable, independently of its case.

 


 

mat_t *Mat_t() const

Returns the raw mat_t* file handle for low-level operations.

 


 

MatVar GetVar(const char *name, bool nocase = false) const

Reads a top-level variable by name and returns an owning MatVar; this method throws if the file is not open or the variable cannot be found. nocase allows to input variable name independently of its case.

 


 

MatVar GetVar(const Vector<String>& names, bool nocase = false) const

Reads a top-level variable from a list of synonym names and returns an owning MatVar; this method throws if the file is not open or the variable cannot be found. nocase allows to input variable name independently of its case.

 


 

bool Exist(const char *name) const

Returns true if the variable  name exists.

 


 

void Write(const char *name, int v, matio_compression compression = MAT_COMPRESSION_NONE)

Writes an integer scalar to the file.

 


 

void Write(const char *name, double v, matio_compression compression = MAT_COMPRESSION_NONE)

Writes a double-precision scalar to the file.

 


 

void Write(const char *name, const char *s, matio_compression compression = MAT_COMPRESSION_NONE)

Writes a character vector using UTF‑8 (when available) or as uint8.

 


 

void WriteColMajor(const char *name, size_t rows, size_t cols, const double *colMajor, matio_compression compression = MAT_COMPRESSION_NONE)

Writes a double matrix from column-major memory.

 


 

void WriteColMajor(const char *name, size_t rows, size_t cols, const int *colMajor, matio_compression compression = MAT_COMPRESSION_NONE)

name rows cols colMajor MAT_COMPRESSION_NONE .

 


 

template <class T> void WriteRowMajor(const char *name, size_t rows, size_t cols, const T *rowMajor, matio_compression compression = MAT_COMPRESSION_NONE)

Writes an integer matrix from column-major memory.

 

 

Constructor Detail

 

MatFile()

Constructs an empty file wrapper.

 


 

~MatFile()

Closes the file handle if it is open.

 


 

MatFile(const MatFile&)

Copy operations are disabled to avoid double-close or shared handle issues.

 


 

MatFile(MatFile&& o)

Move operations transfer ownership of the file handle and path..