Skip to content
This repository was archived by the owner on May 31, 2020. It is now read-only.
/ matf Public archive

Library to extract information from MAT-files into golang structures

License

Notifications You must be signed in to change notification settings

florianl/matf

Repository files navigation

matf Build Status Coverage Status Go Report Card GoDoc

This is matf and it is written in golang. matf extracts the content from MATLABs MAT-files.

Why?

The idea of this project is, to make the content of mat-files available in golang.

Example

Load a simple matrix, saved in a matf-file, in gonum/mat.

package main

import (
	"fmt"
	"io"
	"log"
	"os"
	"reflect"

	"github.com/florianl/matf"
	"gonum.org/v1/gonum/mat"
)

func main() {

	modelfile, err := matf.Open(os.Args[1])
	if err != nil {
		log.Fatal(err)
		return
	}
	defer matf.Close(modelfile)

	element, err := matf.ReadDataElement(modelfile)
	if err != nil && err != io.EOF {
		log.Fatal(err)
		return
	}
	r, c, _, err := element.Dimensions()
	data := []float64{}
	slice := reflect.ValueOf(element.Content.(matf.NumPrt).RealPart)
	for i := 0; i < slice.Len(); i++ {
		value := reflect.ValueOf(slice.Index(i).Interface()).Float()
		data = append(data, value)
	}

	dense := mat.NewDense(r, c, data)
	fmt.Printf("dense = %v\n", mat.Formatted(dense, mat.Prefix("        ")))

}

Simple example, using gorgonia.

package main

import (
	"io"
	"log"
	"os"
	"reflect"

	"github.com/florianl/matf"
	"gorgonia.org/gorgonia"
	"gorgonia.org/tensor"

)

func main() {

	modelfile, err := matf.Open(os.Args[1])
	if err != nil {
		log.Fatal(err)
		return
	}
	defer matf.Close(modelfile)

	element, err := matf.ReadDataElement(modelfile)
	if err != nil && err != io.EOF {
		log.Fatal(err)
		return
	}
	r, c, _, err := element.Dimensions()
	data := []float64{}
	slice := reflect.ValueOf(element.Content.(matf.NumPrt).RealPart)
	for i := 0; i < slice.Len(); i++ {
		value := reflect.ValueOf(slice.Index(i).Interface()).Float()
		data = append(data, value)
	}

	t := tensor.New(tensor.WithShape(r,c), tensor.WithBacking(data))

	g := gorgonia.NewGraph()
	w := gorgonia.NewMatrix(g, gorgonia.Float64, gorgonia.WithShape(r,c), gorgonia.WithValue(t))
	gorgonia.Must(gorgonia.Sigmoid(w))

	m := gorgonia.NewTapeMachine(g)
	if err := m.RunAll(); err != nil {
		log.Fatal(err)
		return
	}

	m.Reset()
}

About

Library to extract information from MAT-files into golang structures

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages