Monday 27 July 2009

Write Matlab files without Matlab via Java and JMatIO

I had to interface a shiny new analytical suite built on KNIME with some legacy applications in Matlab we are unfortunately still using. This demanded exporting tables into Matlab's proprietary data format from Java.

I used the JMatIO library and everything worked like a charm. The following code snippet (taken from here) best illustrates the use of the library:

//1. First create example arrays
double[] src = new double[] { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 };
MLDouble mlDouble = new MLDouble( "double_arr", src, 3 );
MLChar mlChar = new MLChar( "char_arr", "I am dummy" );

//2. write arrays to file
ArrayList list = new ArrayList();
list.add( mlDouble );
list.add( mlChar );

new MatFileWriter( "mat_file.mat", list );

This is equivalent to the following Matlab code:

double_arr = [ 1 2; 3 4; 5 6];
char_arr = 'I am dummy';
save('mat_file.mat', 'double_arr', 'char_arr');

Neat, isn't it?

No comments:

Post a Comment