Git Product home page Git Product logo

mat4js's Introduction

mat4js

JavaScript library to load Matlab Level 5 MAT-files as JavaScript objects. Based on the documentation: https://www.mathworks.com/help/pdf_doc/matlab/matfile_format.pdf released for 2019b

Installation

via npm

$ npm install mat4js

then use as

import { read as readmat } from "mat-for-js"
readmat(ArrayBuffer)
import * from "mat-for-js"
mat4js.read(ArrayBuffer)

via script tag

Download dist/mat4js.read.min.js to your webserver and include it in your HTML:

<script type="text/javascript" src="./mat4js.read.min.js"></script>

Read

Use mat4js.read(ArrayBuffer).

It returns a JavaScript object with a .header and .data property.

.header

contains 116 bytes of text.

Padding characters are not truncated.

.data

is an object containing the named arrays in the file. ({name: content})

Matlab stores vectors as 2D but flat arrays (1xn or nx1). These are converted to plain 1D arrays ([1, 2, 3] instead of [[1, 2, 3]]) or strings in case of character arrays. In multidimensional character arrays the characters are not concatenated to form strings, but are kept separate. ({char_array: [["a", "b", "c"], ["d", "e", "f"]]} instead of {char_array: ["abc", "def"]})

Numeric arrays with Int64 or Uint64 data types are converted into an array of BigInt.

Numeric arrays with imaginary component are converted into an array of objects with .r and .i properties for the real and imaginary components respectively.

Scalar structs are converted into objects as would be expected, while non-scalar structs are more akin to cell arrays. For a struct constructed with

S = struct()
S.cell = {1 2; 3 4}
S.fruit = 'apple'

In JavaScript

S.cell[1][0] == 3
S.fruit == "apple"

But for one constructed with S = struct('cell', {1 2; 3 4}, 'fruit', 'apple') (non-scalar):

S[1][0].cell == 3
S[1][1].cell == 4
S[1][0].fruit == "apple"
S[1][1].fruit == "apple"

and S.fruit == "apple" is not accessible.

Sparse arrays are converted to objects with .x and .y properties describing the width and height of the array respectively, and an .nz property containing an array of objects representing non-zero values. These objects also have .x and .y properties for their indices in the matrix and an additional .v for the non-zero value at the index.

Build

If you want to rebuild dist/readmat.min.js, first install the dev dependencies with npm install. Once webpack is installed, run npm run build.

Limitaions

There is no support for Object array types. 64-bit integer type support depends on using a version of JavaScript that includes the DataView.getBigInt64()/DataView.getBigUint64() methods.

Matlab v7.3 MAT-files use HDF5 data structure and are not supported. Reading such files will throw a FeatureError with the .feature property set to "HDF5". There are other JavaScript projects to view HDF5 files: https://github.com/usnistgov/jsfive

There is no write functionality.

mat4js's People

Contributors

dependabot[bot] avatar kovacsgg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

mat4js's Issues

Why can't support 64-bit data? (Maybe it's the problem caused by the null value)

FeatureError: The MAT file has features that are not supported. See Limitations on the project's page: Array's type is 14, 'mxINT64_CLASS' (unsupported)

My cell format roughly as follows:

[1,2,3,4,5,6,7] [] [] [] [] [] [] [] []
[8,9,10] [11,12,13] [14,15,16,17,18,19,20,21,22,23] [] [] [] [] [] []
24 25 [26,27] 28 29 [30,31,32,33,34] [] [] []
35 [36,37] [38,39,40] [] [] [] [] [] []
124 [125,126] [] [] [] [] [] [] []
41 [42,43,44] [45,46] [] [] [] [] [] []
47 [48,49,50,51] 52 [53,54,55] 56 [57,58,59] 60 [61,62,63] [64,65]

My matlab is R2021a.

Need test cases

The current test cases need to be replaced by more systematic ones that can be automated.

I'm not actually an avid user of matlab, so I'd appreciate some help with this. @VivekTRamamoorthy, sorry for calling out for you, publicly at that (as much as it is), I couldn't find a direct line to you. You expressed interest in this project, and also have a matlab-related project. Would you be willing to construct various .mat files, some to specification, some creatively?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.