Awesome Go

matlab

CategoryScience and Data Analysis
SubcategoryScience and Data Analysis
Stars12

Pure Go library for reading and writing MATLAB .mat files (v5-v7.3) without CGO

About matlab

MATLAB File Reader/Writer for Go

A pure Go implementation of the MATLAB .mat file formats: read and write MAT-files without MATLAB, the MATLAB runtime or cgo. Part of the SciGoLib scientific computing libraries.

GitHub Release Go Version Go Reference GitHub Actions Codecov Go Report Card License GitHub Stars GitHub Issues Discussions

A MAT-file is the file format MATLAB uses to save workspace variables. Engineers and scientists use it to exchange measurements, simulation results and trained parameters, and many tools besides MATLAB read and write it: GNU Octave, SciPy, and libraries for most languages. There are two families. Version 5 (MATLAB 5 to 7.2) is a binary tag-length-value format, optionally zlib-compressed. Version 7.3 is an HDF5 file with a 512-byte MAT header in front. This library reads both and writes both, so a Go program can exchange data with MATLAB users without a C toolchain.


Features

  • Pure Go: no cgo and no C dependencies; cross-compiles like any Go program. 32-bit builds (linux/386, linux/arm, windows/386) are checked in CI.
  • One entry point for reading: matlab.Open detects v5 and v7.3 by the MAT header.
  • Two writers: matlab.Create with matlab.Version5 or matlab.Version73.
  • Numeric data: double, single, int8 to int64 and uint8 to uint64, multi-dimensional, real and complex.
  • Compressed v5 files are read (zlib), with a cap on the decompressed size.
  • Both byte orders in v5 files, read and write.
  • Hardened input handling: sizes read from a file are checked against the bytes present before memory is allocated, and the v5 parser is fuzz-tested.
  • Checked against references: SciPy test files, golden-byte tests of the v5 writer, h5dump on generated v7.3 files; see UPSTREAM.md.

Quick Start

go get github.com/scigolib/matlab

Read a file

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/scigolib/matlab"
)

func main() {
	file, err := os.Open("data.mat")
	if err != nil {
		log.Fatal(err)
	}
	defer file.Close()

	// Detects v5 and v7.3 by itself.
	mat, err := matlab.Open(file)
	if err != nil {
		log.Fatal(err)
	}

	for _, v := range mat.Variables {
		fmt.Printf("%s: %s %v\n", v.Name, v.DataType, v.Dimensions)

		if data, err := v.GetFloat64Array(); err == nil {
			fmt.Println("data:", data)
		}
	}
}

Write a file

package main

import (
	"log"

	"github.com/scigolib/matlab"
	"github.com/scigolib/matlab/types"
)

func main() {
	// matlab.Version5 writes the traditional format instead.
	writer, err := matlab.Create("output.mat", matlab.Version73)
	if err != nil {
		log.Fatal(err)
	}

	err = writer.WriteVariable(&types.Variable{
		Name:       "mydata",
		Dimensions: []int{3, 2},
		DataType:   types.Double,
		Data:       []float64{1, 2, 3, 4, 5, 6}, // column-major
	})
	if err != nil {
		_ = writer.Close()
		log.Fatal(err)
	}

	// Close finishes the file: check its error, do not defer it away.
	if err := writer.Close(); err != nil {
		log.Fatal(err)
	}
}

Complex data, byte order and the file description are covered in the Quick Start guide; runnable programs are in examples.


Documentation

Getting started

Reference

Advanced


Current Status

v0.3.19. Test coverage from go test -cover: 97.1% (root package), 97.4% (internal/v5), 86.8% (internal/v73), 100% (types).

Reading

Featurev5 (v5 to v7.2)v7.3 (HDF5)
Numeric arraysyesyes, returned as []float64
Complex numbersyesgroup layout only (see below)
Character arraysDataType Char; Data is a string or []uint16 by storage typeDataType Char when MATLAB_class is char
Multi-dimensionalyesreported as one-dimensional
Compressionyes (zlib)filters are decoded by the hdf5 module
Structuresclass onlyone variable per field
Cell arraysclass onlyclass only
Sparse matricesnot decodednot supported
  • A v7.3 file may start with MATLAB's 512-byte MAT header; Open reads it (v0.3.18). A bare HDF5 file also opens.
  • A MAT header whose version is neither 0x0100 nor 0x0200 fails with ErrUnsupportedVersion; input that is neither a MAT header nor HDF5 fails with ErrInvalidFormat; a truncated v5 file fails with an error that wraps io.ErrUnexpectedEOF.

Writing

Featurev5v7.3
Numeric arrays, all ten typesyesyes
Complex numbersyesgroup layout, not MATLAB's (see below)
Multi-dimensionalyesyes
Byte orderWithEndianness, little-endian by defaultn/a
Character arraysnot supportednot supported
Structures, cell arraysnot supportednot supported
Compressionnot supportednot supported
  • v7.3 files start with the 512-byte MAT header MATLAB writes in front of its own v7.3 files (v0.3.19).
  • WithCompression is reserved and has no effect.

Validation

  • 283 top-level tests pass (go test -v ./..., counting --- PASS lines of top-level tests, run on 2026-11-07).
  • v5 reading is checked against four reference files from SciPy's test data; the v5 writer against golden byte tests.
  • Generated v7.3 files are opened with h5dump 1.14.6 locally (not in CI), and every generated file must open in the hdf5 module without a warning.
  • MATLAB itself is not available to the maintainers: loading our files in MATLAB is not verified.

Limitations

  • v7.3 complex arrays are written and read as an HDF5 group with real and imag datasets and a MATLAB_complex attribute. MATLAB stores them as one dataset of a compound type with real and imag members; that layout is not read or written, so MATLAB-written complex v7.3 arrays are not recognized as complex.
  • Structures and cell arrays are not decoded. In v5, DataType is Struct or CellArray and Data is not usable; an empty v5 cell array fails to open. In v7.3 a structure comes back as one variable per field, named by its path (/s/a).
  • Sparse matrices are not decoded: a v5 sparse matrix comes back with DataType Unknown and meaningless data.
  • v7.3 reading returns numeric data as []float64 whatever the class, and reports one-dimensional dimensions.
  • Character arrays cannot be written; WriteVariable returns an error for types.Char.
  • A v5 compressed element may decompress to at most 100 MB, at a ratio of at most 1000:1; larger ones are rejected.
  • Function handles and objects are not supported.

See ROADMAP.md for what comes next and docs/guides/DATATYPES.md for the details.


Development

Requirements

  • Go 1.27 or newer. We support only Go releases that still receive security fixes (1.26 and 1.27 today). Go 1.27 also enables generic methods, which let one type-safe method replace a family of per-type methods — so the API stays small. We use them only where they replace real duplication.
  • github.com/scigolib/hdf5 for v7.3, a pure Go module that go fetches
  • No C dependencies. The race detector needs cgo, so make test-race needs a C compiler.

Building and testing

git clone https://github.com/scigolib/matlab.git
cd matlab

make build        # go build ./...
make test         # tests with coverage
make lint         # golangci-lint
make cross-build  # 32-bit targets
make check        # formatting check, tests, lint, 32-bit builds (local subset; CI is the gate)

go run ./scripts/generate-testdata   # regenerate the v7.3 test files
go run ./scripts/verify-roundtrip    # write-and-read check

Test data

  • testdata/scipy/: 4 reference files from the SciPy project
  • testdata/generated/: 8 v7.3 files written by our writer
  • testdata/legacy/: 1 v7.3 file written by an early release, kept to prove it still reads

Contributing

Contributions are welcome: bug reports with a sample file, fixes, documentation, and MAT-files from your own field that the library should read. Before a pull request:

  1. Read CONTRIBUTING.md for the workflow and the checks
  2. Check the open issues
  3. Look at the architecture overview

Open areas: reading and writing v7.3 complex arrays in MATLAB's compound layout, decoding structures and cell arrays, reading sparse matrices, writing character arrays, and compression in the writers.


Other Go libraries

This libraryready-steady/mat
Descriptionreader and writer of MAT-files, v5 and v7.3"Reader and writer of MATLAB MAT-files" (repository description)
Repositoryactivelast change March 2015

The comparison covers only what the repository pages state; the other library was not tested. For HDF5 itself see scigolib/hdf5, which this library uses for v7.3.


MATLAB Resources


License

MIT License; see LICENSE.


Acknowledgments

  • The MathWorks, for the MAT-file format documentation
  • The HDF Group, for the HDF5 format and the C library the v7.3 files are checked against
  • The SciPy project, for the reference test files
  • matio, whose handling of the MAT header we follow
  • Everyone who reported issues and contributed code

Support


Star History

Frequently Asked Questions

What is matlab?

matlab is a Science and Data Analysis library for the Go programming language. Pure Go library for reading and writing MATLAB .mat files (v5-v7.3) without CGO

How do I install matlab?

Install matlab with the Go module system using `go get scigolib/matlab`. Check the repository for the current installation instructions.

What category does matlab belong to?

matlab is listed under Science and Data Analysis, specifically Science and Data Analysis.

← Back to Science and Data Analysis