matlab

package module
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2025 License: MIT Imports: 9 Imported by: 1

README ยถ

MATLAB File Reader/Writer for Go

Pure Go implementation for reading AND writing MATLAB .mat files - No CGo required

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


A modern, pure Go library for reading and writing MATLAB .mat files without CGo dependencies. Part of the SciGoLib scientific computing ecosystem.

Features

โœจ Read & Write Support

  • ๐Ÿ“– Read MATLAB v5-v7.2 files (traditional format)
  • ๐Ÿ“– Read MATLAB v7.3+ files (HDF5 format)
  • โœ๏ธ Write MATLAB v7.3+ files (HDF5 format)
  • โœ๏ธ Write MATLAB v5-v7.2 files (traditional format) - NEW in v0.2.0!

๐ŸŽฏ Key Capabilities

  • Simple, intuitive API
  • Zero external C dependencies
  • Type-safe data access
  • Comprehensive error handling
  • Round-trip verified (write โ†’ read โ†’ verify)

๐Ÿ“Š Data Types

  • All numeric types (double, single, int8-64, uint8-64)
  • Complex numbers
  • Multi-dimensional arrays
  • Character arrays
  • Structures
  • Cell arrays
  • Sparse matrices
  • Compressed data (reading)

Installation

go get github.com/scigolib/matlab

Quick Start

Reading MAT-Files
package main

import (
	"fmt"
	"log"
	"os"

	"github.com/scigolib/matlab"
)

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

	// Parse MAT-file (auto-detects v5 or v7.3)
	mat, err := matlab.Open(file)
	if err != nil {
		log.Fatal(err)
	}

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

		// Access data based on type
		if data, ok := v.Data.([]float64); ok {
			fmt.Println("Data:", data)
		}
	}
}
Writing MAT-Files
v7.3 Format (HDF5-based)
package main

import (
	"log"

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

func main() {
	// Create new MAT-file (v7.3 format)
	writer, err := matlab.Create("output.mat", matlab.Version73)
	if err != nil {
		log.Fatal(err)
	}
	defer writer.Close()

	// Write a variable
	err = writer.WriteVariable(&types.Variable{
		Name:       "mydata",
		Dimensions: []int{3, 2},
		DataType:   types.Double,
		Data:       []float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0},
	})
	if err != nil {
		log.Fatal(err)
	}

	// Write complex numbers
	err = writer.WriteVariable(&types.Variable{
		Name:       "z",
		Dimensions: []int{2},
		DataType:   types.Double,
		IsComplex:  true,
		Data: &types.NumericArray{
			Real: []float64{1.0, 2.0},
			Imag: []float64{3.0, 4.0},
		},
	})
	if err != nil {
		log.Fatal(err)
	}
}
v5 Format (Traditional Binary) - NEW in v0.2.0!
package main

import (
	"log"

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

func main() {
	// Create new MAT-file (v5 format - legacy compatible)
	writer, err := matlab.Create("output_v5.mat", matlab.Version5)
	if err != nil {
		log.Fatal(err)
	}
	defer writer.Close()

	// Write a simple array
	err = writer.WriteVariable(&types.Variable{
		Name:       "A",
		Dimensions: []int{3},
		DataType:   types.Double,
		Data:       []float64{1.0, 2.0, 3.0},
	})
	if err != nil {
		log.Fatal(err)
	}

	// Write a matrix (multi-dimensional)
	err = writer.WriteVariable(&types.Variable{
		Name:       "B",
		Dimensions: []int{2, 3},
		DataType:   types.Double,
		Data:       []float64{1, 2, 3, 4, 5, 6},
	})
	if err != nil {
		log.Fatal(err)
	}

	// Write complex numbers
	err = writer.WriteVariable(&types.Variable{
		Name:       "C",
		Dimensions: []int{2},
		DataType:   types.Double,
		IsComplex:  true,
		Data: &types.NumericArray{
			Real: []float64{1.0, 2.0},
			Imag: []float64{3.0, 4.0},
		},
	})
	if err != nil {
		log.Fatal(err)
	}
}

Supported Features

Reader Support
Feature v5 (v5-v7.2) v7.3+ (HDF5)
Numeric arrays โœ… โœ…
Complex numbers โœ… โœ…
Character arrays โœ… โœ…
Multi-dimensional โœ… โœ…
Structures โœ… โœ…
Cell arrays โœ… โœ…
Sparse matrices โœ… โœ…
Compression โœ… โœ…
Function handles โŒ Out of scope โŒ Out of scope
Objects โŒ Out of scope โŒ Out of scope
Writer Support
Feature v5 (v5-v7.2) v7.3+ (HDF5)
Numeric arrays โœ… โœ…
Complex numbers โœ… โœ…
Character arrays โœ… โœ…
Multi-dimensional โœ… โœ…
Both endianness โœ… MI/IM N/A
Structures ๐Ÿ“… v0.5.0+ ๐Ÿ“… v0.5.0+
Cell arrays ๐Ÿ“… v0.5.0+ ๐Ÿ“… v0.5.0+
Compression ๐Ÿ“… v0.5.0+ ๐Ÿ“… v0.5.0+

Known Limitations

Writer Limitations
  • No compression support (planned for v0.5.0+)
  • No structures/cell arrays writing (planned for v0.5.0+)
Reader Limitations
  • Function handles not supported (MATLAB-specific, cannot be serialized)
  • Objects not supported (language-specific)
What Works Well โœ…
  • โœ… v5 Writer COMPLETE - All numeric types, complex numbers, multi-dimensional arrays
  • โœ… v7.3 Writer COMPLETE - Full HDF5-based writing
  • โœ… Parser bugs FIXED - Multi-dimensional arrays, multiple variables
  • โœ… All numeric types (double, single, int8-64, uint8-64)
  • โœ… Multi-dimensional arrays (read & write)
  • โœ… Complex numbers (proper MATLAB format for both v5 and v7.3)
  • โœ… Round-trip verified (v5 write โ†’ read, v7.3 write โ†’ read)
  • โœ… Cross-platform (Windows, Linux, macOS)
  • โœ… Both endianness (MI/IM for v5)

See CHANGELOG.md for detailed limitations and planned fixes.

Documentation

Development

Requirements
  • Go 1.25 or later
  • HDF5 library (for v7.3+ support): github.com/scigolib/hdf5 develop branch (commit 36994ac)
  • No external C dependencies
Building
# Clone repositories (side-by-side)
cd D:\projects\scigolibs
git clone https://github.com/scigolib/hdf5.git
git clone https://github.com/scigolib/matlab.git

# Build MATLAB library
cd matlab
make build

# Run tests
make test

# Run linter
make lint

# Generate test data
go run scripts/generate-testdata/main.go

# Verify round-trip
go run scripts/verify-roundtrip/main.go
Testing
# Run all tests
make test

# Run with coverage
make test-coverage

# Run specific tests
go test ./internal/v73 -v

# Run linter
make lint
Test Data

The project includes test data in testdata/:

  • testdata/generated/ - Files created by our writer (8 files)
  • testdata/scipy/ - Reference files from SciPy project (3 files)

Contributing

Contributions are welcome! This is a stable project and we'd love your help.

Before contributing:

  1. Read CONTRIBUTING.md - Git workflow and development guidelines
  2. Check open issues
  3. Review the architecture in .claude/CLAUDE.md

Ways to contribute:

  • ๐Ÿ› Report bugs
  • ๐Ÿ’ก Suggest features
  • ๐Ÿ“ Improve documentation
  • ๐Ÿ”ง Submit pull requests
  • โญ Star the project
  • ๐Ÿงช Test with real MATLAB files and report compatibility

Priority Areas:

  • Test MATLAB/Octave compatibility with real-world files
  • Add compression support for v5/v7.3 writers
  • Implement structures and cell arrays writing
  • Improve test coverage (current: 85.4%, target: 90%+)

Comparison with Other Libraries

Feature This Library go-hdf5/* matlab-go
Pure Go โœ… Yes โŒ CGo required โœ… Yes
v5-v7.2 Read โœ… Yes โŒ Limited โš ๏ธ Partial
v7.3+ Read โœ… Yes โŒ No โŒ No
Write Support โœ… v5 + v7.3 Yes โŒ No โŒ No
Complex Numbers โœ… Yes โš ๏ธ Limited โŒ No
Maintained โœ… Active โŒ Inactive โŒ Inactive
Cross-platform โœ… Yes โš ๏ธ Platform-specific โœ… Yes

  • HDF5 Go Library - Pure Go HDF5 implementation (used for v7.3+ support)
  • Part of SciGoLib - Scientific computing libraries for Go

License

This project is licensed under the MIT License - see the LICENSE file for details.


Acknowledgments

  • The MathWorks for the MATLAB file format specification
  • The HDF Group for HDF5 format specification
  • scigolib/hdf5 for HDF5 support
  • SciPy project for reference test data
  • All contributors to this project

Support

  • ๐Ÿ“– Documentation - See .claude/CLAUDE.md for architecture details
  • ๐Ÿ› Issue Tracker
  • ๐Ÿ’ฌ Discussions - GitHub Discussions (coming soon)
  • ๐Ÿ“ง Contact - Via GitHub Issues

Status: โœ… STABLE - Production-ready read and write support for both v5 and v7.3 formats! Last Updated: 2025-11-21

Ready for: Production use, testing, feedback, and real-world usage Stable API: Minor API changes may occur in 0.x versions, major stability expected


Built with โค๏ธ by the SciGoLib community

Documentation ยถ

Overview ยถ

Package matlab provides a library for reading and writing MATLAB .mat files. It supports both v5 (MATLAB v5-v7.2) and v7.3+ (HDF5-based) formats.

Example ยถ

Example demonstrates basic usage of the MATLAB file library.

package main

import (
	"fmt"
	"os"
	"path/filepath"

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

func main() {
	tmpfile := filepath.Join(os.TempDir(), "example.mat")
	defer os.Remove(tmpfile)

	// Create a simple v5 MATLAB file
	writer, _ := matlab.Create(tmpfile, matlab.Version5)
	defer writer.Close()

	// Write a variable
	writer.WriteVariable(&types.Variable{
		Name:       "data",
		Dimensions: []int{3},
		DataType:   types.Double,
		Data:       []float64{1.0, 2.0, 3.0},
	})

	fmt.Println("MATLAB file created successfully")
}
Output:

MATLAB file created successfully

Index ยถ

Examples ยถ

Constants ยถ

This section is empty.

Variables ยถ

View Source
var ErrInvalidFormat = errors.New("invalid MAT-file format")

ErrInvalidFormat indicates an invalid MAT-file format.

View Source
var ErrUnsupportedVersion = errors.New("unsupported MAT-file version")

ErrUnsupportedVersion indicates an unsupported MAT-file version.

Functions ยถ

This section is empty.

Types ยถ

type MatFile ยถ

type MatFile struct {
	Version     string            // MAT-file version (e.g., "5.0", "7.3")
	Endian      string            // Byte order indicator ("MI" or "IM")
	Description string            // File description from header
	Variables   []*types.Variable // List of variables in the file
}

MatFile represents a parsed MAT-file.

func Open ยถ

func Open(r io.Reader) (*MatFile, error)

Open reads and parses a MAT-file from an io.Reader.

Example ยถ

ExampleOpen demonstrates reading a MATLAB file.

package main

import (
	"fmt"
	"os"

	"github.com/scigolib/matlab"
)

func main() {
	file, _ := os.Open("testdata/generated/simple_double.mat")
	defer file.Close()

	matFile, _ := matlab.Open(file)

	fmt.Printf("Found %d variable(s)\n", len(matFile.Variables))
}
Output:

Found 1 variable(s)
Example (MultipleVariables) ยถ

ExampleOpen_multipleVariables demonstrates handling multiple variables.

package main

import (
	"fmt"
	"os"
	"path/filepath"

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

func main() {
	tmpfile := filepath.Join(os.TempDir(), "example_multi.mat")
	defer os.Remove(tmpfile)

	// Write multiple variables
	writer, _ := matlab.Create(tmpfile, matlab.Version5)
	writer.WriteVariable(&types.Variable{
		Name:       "x",
		Dimensions: []int{3},
		DataType:   types.Double,
		Data:       []float64{1, 2, 3},
	})
	writer.WriteVariable(&types.Variable{
		Name:       "y",
		Dimensions: []int{3},
		DataType:   types.Double,
		Data:       []float64{4, 5, 6},
	})
	writer.Close()

	// Read all variables
	file, _ := os.Open(tmpfile)
	defer file.Close()

	matFile, _ := matlab.Open(file)
	fmt.Printf("Total variables: %d\n", len(matFile.Variables))
	for _, v := range matFile.Variables {
		fmt.Printf("- %s\n", v.Name)
	}
}
Output:

Total variables: 2
- x
- y
Example (RoundTrip) ยถ

ExampleOpen_roundTrip demonstrates writing and reading back data.

package main

import (
	"fmt"
	"os"
	"path/filepath"

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

func main() {
	tmpfile := filepath.Join(os.TempDir(), "example_roundtrip.mat")
	defer os.Remove(tmpfile)

	// Write
	writer, _ := matlab.Create(tmpfile, matlab.Version5)
	writer.WriteVariable(&types.Variable{
		Name:       "test",
		Dimensions: []int{2},
		DataType:   types.Double,
		Data:       []float64{3.14, 2.71},
	})
	writer.Close()

	// Read
	file, _ := os.Open(tmpfile)
	defer file.Close()

	matFile, _ := matlab.Open(file)
	data := matFile.Variables[0].Data.([]float64)

	fmt.Printf("Read back: %.2f, %.2f\n", data[0], data[1])
}
Output:

Read back: 3.14, 2.71

func (*MatFile) GetVariable ยถ added in v0.3.0

func (m *MatFile) GetVariable(name string) *types.Variable

GetVariable retrieves a variable by name. Returns nil if the variable is not found.

Example:

matFile, _ := matlab.Open(file)
data := matFile.GetVariable("mydata")
if data != nil {
    fmt.Println("Found:", data.Name)
}

func (*MatFile) GetVariableNames ยถ added in v0.3.0

func (m *MatFile) GetVariableNames() []string

GetVariableNames returns the names of all variables in the file. Useful for listing available data without accessing values.

Example:

matFile, _ := matlab.Open(file)
names := matFile.GetVariableNames()
fmt.Println("Variables:", names) // ["x", "y", "z"]

func (*MatFile) HasVariable ยถ added in v0.3.0

func (m *MatFile) HasVariable(name string) bool

HasVariable checks if a variable with the given name exists.

Example:

if matFile.HasVariable("results") {
    data := matFile.GetVariable("results")
    // process data
}

type MatFileWriter ยถ

type MatFileWriter struct {
	// contains filtered or unexported fields
}

MatFileWriter represents a MATLAB file opened for writing.

The writer automatically selects the appropriate backend based on the requested version (v5 or v7.3). After creating a writer, use WriteVariable to add variables to the file, then Close to finalize.

func Create ยถ

func Create(filename string, version Version, opts ...Option) (*MatFileWriter, error)

Create creates a new MATLAB file for writing with optional configuration.

The version parameter specifies the format: Version5 or Version73. Optional parameters can be provided using functional options.

Supported options:

  • WithEndianness(binary.ByteOrder) - v5 byte order (default: LittleEndian)
  • WithDescription(string) - v5 file description (max 116 bytes)
  • WithCompression(int) - compression level 0-9 (not yet implemented)

Example (basic):

writer, err := matlab.Create("output.mat", matlab.Version5)

Example (with options):

writer, err := matlab.Create("output.mat", matlab.Version5,
    matlab.WithEndianness(binary.BigEndian),
    matlab.WithDescription("Simulation results"))

Example (v7.3):

writer, err := matlab.Create("output.mat", matlab.Version73)
Example ยถ

ExampleCreate demonstrates creating a MATLAB file.

package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/scigolib/matlab"
)

func main() {
	tmpfile := filepath.Join(os.TempDir(), "example_create.mat")
	defer os.Remove(tmpfile)

	writer, err := matlab.Create(tmpfile, matlab.Version5)
	if err != nil {
		panic(err)
	}
	defer writer.Close()

	fmt.Println("File created")
}
Output:

File created
Example (V5) ยถ

ExampleCreate_v5 demonstrates creating a v5 format file.

package main

import (
	"fmt"
	"os"
	"path/filepath"

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

func main() {
	tmpfile := filepath.Join(os.TempDir(), "example_v5.mat")
	defer os.Remove(tmpfile)

	writer, _ := matlab.Create(tmpfile, matlab.Version5)
	defer writer.Close()

	writer.WriteVariable(&types.Variable{
		Name:       "matrix",
		Dimensions: []int{2, 3},
		DataType:   types.Double,
		Data:       []float64{1, 2, 3, 4, 5, 6},
	})

	fmt.Println("v5 file created")
}
Output:

v5 file created
Example (V73) ยถ

ExampleCreate_v73 demonstrates creating a v7.3 HDF5 format file.

package main

import (
	"fmt"
	"os"
	"path/filepath"

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

func main() {
	tmpfile := filepath.Join(os.TempDir(), "example_v73.mat")
	defer os.Remove(tmpfile)

	writer, _ := matlab.Create(tmpfile, matlab.Version73)
	defer writer.Close()

	writer.WriteVariable(&types.Variable{
		Name:       "data",
		Dimensions: []int{100},
		DataType:   types.Double,
		Data:       make([]float64, 100),
	})

	fmt.Println("v7.3 file created")
}
Output:

v7.3 file created
Example (WithOptions) ยถ

ExampleCreate_withOptions demonstrates using functional options.

package main

import (
	"encoding/binary"
	"fmt"
	"os"
	"path/filepath"

	"github.com/scigolib/matlab"
)

func main() {
	tmpfile := filepath.Join(os.TempDir(), "options.mat")
	defer os.Remove(tmpfile)

	writer, _ := matlab.Create(tmpfile, matlab.Version5,
		matlab.WithEndianness(binary.BigEndian),
		matlab.WithDescription("Simulation results"),
	)
	defer writer.Close()

	fmt.Println("File created with custom options")
}
Output:

File created with custom options

func (*MatFileWriter) Close ยถ

func (w *MatFileWriter) Close() error

Close closes the MATLAB file and flushes all data to disk.

After calling Close, the writer cannot be used anymore. Any subsequent calls to WriteVariable or Close will fail.

It is safe to call Close multiple times - subsequent calls will be no-ops.

Returns:

  • error: If flushing or closing fails

func (*MatFileWriter) WriteVariable ยถ

func (w *MatFileWriter) WriteVariable(v *types.Variable) error

WriteVariable writes a variable to the MATLAB file.

The variable must have valid Name, Dimensions, DataType, and Data fields. The data is written immediately to the underlying storage.

Parameters:

  • v: Variable to write (must not be nil)

Returns:

  • error: If writing fails or variable is invalid

Supported types:

  • Double, Single (float64, float32)
  • Int8, Uint8, Int16, Uint16, Int32, Uint32, Int64, Uint64
  • Complex numbers (use types.NumericArray with Real/Imag)

Example:

// Simple array
writer.WriteVariable(&types.Variable{
    Name:       "A",
    Dimensions: []int{2, 3},
    DataType:   types.Double,
    Data:       []float64{1, 2, 3, 4, 5, 6},
})

// Complex numbers
writer.WriteVariable(&types.Variable{
    Name:       "C",
    Dimensions: []int{2},
    DataType:   types.Double,
    IsComplex:  true,
    Data: &types.NumericArray{
        Real: []float64{1.0, 3.0},
        Imag: []float64{2.0, 4.0},
    },
})
Example ยถ

ExampleMatFileWriter_WriteVariable demonstrates writing a simple array.

package main

import (
	"fmt"
	"os"
	"path/filepath"

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

func main() {
	tmpfile := filepath.Join(os.TempDir(), "example_array.mat")
	defer os.Remove(tmpfile)

	writer, _ := matlab.Create(tmpfile, matlab.Version5)
	defer writer.Close()

	err := writer.WriteVariable(&types.Variable{
		Name:       "mydata",
		Dimensions: []int{5},
		DataType:   types.Double,
		Data:       []float64{1.0, 2.0, 3.0, 4.0, 5.0},
	})

	if err == nil {
		fmt.Println("Variable written")
	}
}
Output:

Variable written
Example (Complex) ยถ

ExampleMatFileWriter_WriteVariable_complex demonstrates writing complex numbers.

package main

import (
	"fmt"
	"os"
	"path/filepath"

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

func main() {
	tmpfile := filepath.Join(os.TempDir(), "example_complex.mat")
	defer os.Remove(tmpfile)

	writer, _ := matlab.Create(tmpfile, matlab.Version73)
	defer writer.Close()

	complexData := &types.NumericArray{
		Real: []float64{1.0, 2.0, 3.0},
		Imag: []float64{4.0, 5.0, 6.0},
	}

	writer.WriteVariable(&types.Variable{
		Name:       "signal",
		Dimensions: []int{3},
		DataType:   types.Double,
		Data:       complexData,
		IsComplex:  true,
	})

	fmt.Println("Complex variable written")
}
Output:

Complex variable written
Example (Int32) ยถ

ExampleMatFileWriter_WriteVariable_int32 demonstrates writing integer data.

package main

import (
	"fmt"
	"os"
	"path/filepath"

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

func main() {
	tmpfile := filepath.Join(os.TempDir(), "example_integers.mat")
	defer os.Remove(tmpfile)

	writer, _ := matlab.Create(tmpfile, matlab.Version5)
	defer writer.Close()

	writer.WriteVariable(&types.Variable{
		Name:       "counts",
		Dimensions: []int{4},
		DataType:   types.Int32,
		Data:       []int32{10, 20, 30, 40},
	})

	fmt.Println("Integer array written")
}
Output:

Integer array written
Example (Matrix) ยถ

ExampleMatFileWriter_WriteVariable_matrix demonstrates writing a 2D matrix.

package main

import (
	"fmt"
	"os"
	"path/filepath"

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

func main() {
	tmpfile := filepath.Join(os.TempDir(), "example_matrix.mat")
	defer os.Remove(tmpfile)

	writer, _ := matlab.Create(tmpfile, matlab.Version5)
	defer writer.Close()

	// 3x4 matrix in column-major order (MATLAB standard)
	writer.WriteVariable(&types.Variable{
		Name:       "A",
		Dimensions: []int{3, 4},
		DataType:   types.Double,
		Data:       []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
	})

	fmt.Println("Matrix written")
}
Output:

Matrix written

type Option ยถ added in v0.3.0

type Option func(*config)

Option configures optional parameters for Create.

func WithCompression ยถ added in v0.3.0

func WithCompression(level int) Option

WithCompression enables compression with specified level (0-9). 0 = no compression, 9 = maximum compression

Note: Compression is not yet implemented. This option is reserved for future use.

Default: 0 (no compression)

Example:

writer, _ := matlab.Create("file.mat", matlab.Version73,
    matlab.WithCompression(6))

func WithDescription ยถ added in v0.3.0

func WithDescription(desc string) Option

WithDescription sets the file description (v5 only, max 116 bytes). If longer than 116 bytes, it will be truncated.

Default: "MATLAB MAT-file, created by scigolib/matlab vX.X.X"

Example:

writer, _ := matlab.Create("file.mat", matlab.Version5,
    matlab.WithDescription("Simulation results"))
Example ยถ

ExampleWithDescription demonstrates custom file description.

package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/scigolib/matlab"
)

func main() {
	tmpfile := filepath.Join(os.TempDir(), "described.mat")
	defer os.Remove(tmpfile)

	writer, _ := matlab.Create(tmpfile, matlab.Version5,
		matlab.WithDescription("My experimental data from 2025"),
	)
	defer writer.Close()

	fmt.Println("File with custom description created")
}
Output:

File with custom description created

func WithEndianness ยถ added in v0.3.0

func WithEndianness(order binary.ByteOrder) Option

WithEndianness sets the byte order for v5 files. Valid values: binary.LittleEndian, binary.BigEndian

Default: binary.LittleEndian

Example:

writer, _ := matlab.Create("file.mat", matlab.Version5,
    matlab.WithEndianness(binary.BigEndian))
Example ยถ

ExampleWithEndianness demonstrates setting byte order.

package main

import (
	"encoding/binary"
	"fmt"
	"os"
	"path/filepath"

	"github.com/scigolib/matlab"
)

func main() {
	tmpfile := filepath.Join(os.TempDir(), "bigendian.mat")
	defer os.Remove(tmpfile)

	writer, _ := matlab.Create(tmpfile, matlab.Version5,
		matlab.WithEndianness(binary.BigEndian),
	)
	defer writer.Close()

	fmt.Println("Big-endian file created")
}
Output:

Big-endian file created

type Version ยถ

type Version int

Version represents MAT-file format version for writing.

const (
	// Version5 represents v5-v7.2 format (binary format).
	// Recommended for smaller files and maximum compatibility.
	Version5 Version = 5

	// Version73 represents v7.3+ format (HDF5-based).
	// Recommended for large files and modern MATLAB versions.
	Version73 Version = 73
)

Directories ยถ

Path Synopsis
cmd
example command
Package main provides an example of using the MATLAB file reader library.
Package main provides an example of using the MATLAB file reader library.
write-complex command
internal
v5
Package v5 implements MATLAB v5 format parser (v5-v7.2).
Package v5 implements MATLAB v5 format parser (v5-v7.2).
v73
Package v73 provides internal v7.3 MAT-file parsing and writing via HDF5.
Package v73 provides internal v7.3 MAT-file parsing and writing via HDF5.
scripts
generate-testdata command
Package main - Generate minimal test MAT-files
Package main - Generate minimal test MAT-files
verify-roundtrip command
Package main - Critical verification script for v7.3 read/write
Package main - Critical verification script for v7.3 read/write
Package types provides common data structures for MATLAB variables and arrays.
Package types provides common data structures for MATLAB variables and arrays.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL