matlab

package module
v0.0.0-...-1ed1d96 Latest Latest
Warning

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

Go to latest
Published: May 28, 2019 License: MIT Imports: 11 Imported by: 1

README

Matlab

Apparently a parser for matlab 5.0 doesn't exist in go. So, here we are.

The matlab format can be found in this pdf.

This is still very much a work in progress!

Go Report Card Travis CI

Example usage

package main

import (
	"fmt"
	"os"

	"github.com/daniellowtw/matlab"
)

func main() {
	f, err := os.Open("example.mat")
	if err != nil {
		panic(err)
	}
	defer f.Close()
	file, err := matlab.NewFileFromReader(f)
	if err != nil {
		panic(err)
	}
	fmt.Println(file.GetVarsNames()) // prints the variables in the mat file
	matrix, _ := file.GetVar("a")
	// convenient method to marshal into go types
	var _ []int64 = matrix.IntArray()
	floatMatrix, _ := file.GetVar("b")
	// convenient method to marshal into go types
	var _ []float64 = floatMatrix.DoubleArray()
}

Matrix with cells

A CellMatrix is a matrix where the values are matrices. The GetAtLocation method allows indexing into the values array. A convenience method String() on a Matrix is available to convert the CharArray matrix into a string.

Example:

cellMatrix, _ := file.GetVar("a")
firstElement := cellMatrix.GetAtLocation(0).(*Matrix).String()
SecondElement := cellMatrix.GetAtLocation(1).(*Matrix).DoubleArray()

Matrix with struct

Suppose we create a struct with the following:

X.w = [1];
# or X.w = 1

X.y = [2];
X.z = ["abc"];
# or X.z = "abc"

We can read it as follows:

structMatrix, _ := file.GetVar("X")
w := cellMatrix.Struct()["w"].GetAtLocation(0) // float64(1)
z := cellMatrix.Struct()["z"].String() // "abc"

TODO

  • Support sparse array class within miMatrix parser
  • Support object class within miMatrix parser
  • Support writing to mat file

Documentation

Overview

Package matlab defines readers & writers for working with matlab .mat files

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DataType

type DataType uint32

DataType represents matlab data types

const (
	DataTypeUnknown DataType = iota // errored data type
	DTmiINT8                        // 8 bit, signed
	DTmiUINT8                       // 8 bit, unsigned
	DTmiINT16                       // 16-bit, signed
	DTmiUINT16                      // 16-bit, unsigned
	DTmiINT32                       // 32-bit, signed
	DTmiUINT32                      // 32-bit, unsigned
	DTmiSINGLE                      // IEEE® 754 single format

	DTmiDOUBLE // IEEE 754 double format

	DTmiINT64      // 64-bit, signed
	DTmiUINT64     // 64-bit, unsigned
	DTmiMATRIX     // MATLAB array
	DTmiCOMPRESSED // Compressed Data
	DTmiUTF8       // Unicode UTF-8 Encoded Character Data
	DTmiUTF16      // Unicode UTF-16 Encoded Character Data
	DTmiUTF32      // Unicode UTF-32 Encoded Character Data
)

Data Types as specified according to byte indicators

func (DataType) NumBytes

func (d DataType) NumBytes() int

NumBytes returns the number of bytes needed to represent the datatype

func (DataType) String

func (d DataType) String() string

type Element

type Element interface {
	Type() DataType
	Value() []interface{}
}

Element is a parsed matlab data element

type File

type File struct {
	Header *Header
	// contains filtered or unexported fields
}

File represents a .mat matlab file

func NewFileFromReader

func NewFileFromReader(r io.Reader) (f *File, err error)

NewFileFromReader creates a file from a reader and attempts to read the header

func (*File) GetVar

func (f *File) GetVar(name string) (*Matrix, bool)

GetVar returns the variable in the mat file

func (*File) GetVarsNames

func (f *File) GetVarsNames() []string

GetVarsNames returns the list of variables in the given mat file

func (*File) WriteElement

func (f *File) WriteElement(e *Element) error

WriteElement writes a single element to a file's writer

type Flags

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

flags indicating whether the numeric data is complex, global or logical. See 1-16 of specs.

type Header struct {
	Level     string
	Platform  string
	Created   time.Time
	Endianess binary.ByteOrder
}

Header is a matlab .mat file header

func (*Header) String

func (h *Header) String() string

String implements the stringer interface for Header with the standard .mat file prefix (without the filler bytes)

type Matrix

type Matrix struct {
	Name      string
	Dimension []int32 // at least length 2

	Class mxClass
	// contains filtered or unexported fields
}

func (*Matrix) DoubleArray

func (m *Matrix) DoubleArray() []float64

DoubleArray is a convenience method to extract the matrix value as []float64. Warning: It panics if the matlab class is not Double or Single

func (*Matrix) GetAtLocation

func (m *Matrix) GetAtLocation(i int) interface{}

func (*Matrix) IntArray

func (m *Matrix) IntArray() []int64

IntArray is a convenience method to extract the matrix value as []int64. Warning: It panics if the matlab class is not an integer type

func (*Matrix) String

func (m *Matrix) String() []rune

String is a convenience method to extract the matrix value as []rune. Warning: It panics if the matlab class is not mxChar

func (*Matrix) Struct

func (m *Matrix) Struct() map[string]*Matrix

Convenience method to return the struct

func (*Matrix) Type

func (m *Matrix) Type() DataType

func (*Matrix) Value

func (m *Matrix) Value() []interface{}

Jump to

Keyboard shortcuts

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