views:

325

answers:

2

Hello all,

I have some data that I would like to save to a MAT file (version 4 or 5, or any version, for that matter). The catch: I wanted to do this without using matlab libraries, since this code will not necessary run in a machine with matlab. My program uses Java and C++, so any existing library in those languages that achieves this could help me out...

I did some research but did not find anything in Java/C++. However, I found that scipy on python achieves this with mio4.py or mio5.py. I thought about implementing this on java or C++, but it seems a bit out of my time schedule.

So the question is: is there any libraries in Java or C/C++ that permits saving MAT files without using Matlab libraries?

Thanks a lot

+3  A: 

MAT files since version 7 are HDF5 based. I recall that they use some rather funny conventions, but you may be able to reverse engineer what you need. There are certainly HDF5 writing libraries for both Java and C++.

Along these lines, Matlab can read/write several standard formats, including HDF5. It may be easiest to write your data in "standard" HDF5 and read it into the desired data structure within Matlab.

Barry Wark
+1 I thought I'd read that somewhere but can never find confirmation when I go looking. You raise another possibility, OP could read/write HDF5 files which MATLAB can definitely handle whatever it's own implementation of MAT files might be.
High Performance Mark
@HPM Excellent point. I'll mention that.
Barry Wark
HDF5 is row-major. Mat is column-major. The current `help` on hdf5write explains a little bit about why this can be a bad thing
KitsuneYMG
@kts: yes, but the order in which Matlab writes to an HDF5 file can be switched. I've never found this to be a problem in practice, though i have occasionally had to transpose a matrix in Matlab or a Fortran program.
High Performance Mark
+3  A: 

C: matio
Java: jmatio
(I'm really tempted to, so I will, tell you to learn to google)

But really, it's not that hard to write matfiles using fwrite if you don't need to handle some of the more complex stuff (nested structs, classes, functions, sparse matrix, etc).
See: http://www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/matfile_format.pdf

KitsuneYMG