2. unzip and put contents in folder called jmatio
3. rename lib dir to library
3. rename library/jamtio.jar to library/jmatio.jar
4. create a mat file in the sketch data dir called veh_x.mat which contains an array called veh_x
5. Run the following code:
import com.jmatio.io.*;
import com.jmatio.types.*;
MatFileReader mfr = null;
try {
mfr = new MatFileReader(sketchPath + "/data/veh_x.mat" );
} catch (IOException e) {
e.printStackTrace();
exit();
}
if (mfr != null) {
double[][] data = ((MLDouble)mfr.getMLArray( "veh_x" )).getArray();
println(data.length +" " + data[0].length + " " + data[0][0]);
}
TBD use getContents instead of requiring the mat file name and array name be the same.
1 comment:
For those, like I, new to MatLab, these steps may make it more clear what is going on...
4. rename library/jamtio.jar to library/jmatio.jar
5. Open MatLab and create an array called veh_x
veh_x = [1.1,2.2,3.3,4.4]
6. Save the veh_x array into a library dir file called veh_x.mat
save('\path\To\Your\libraryDir\veh_x.mat', 'veh_x')
7. Save (into Veh.java in the library dir), compile (javac -classpath jmatio.jar veh.java),
and run (java -classpath .;jmatio.jar Veh) the following code:
import com.jmatio.io.*;
import com.jmatio.types.*;
public class Veh {
public static void main (String[] args) {
MatFileReader mfr = null;
try {
mfr = new MatFileReader("matlab.mat" );
} catch (java.io.IOException e) {
e.printStackTrace();
System.exit(1);
}
if (mfr != null) {
double[][] data = ((MLDouble)mfr.getMLArray( "veh_x" )).getArray();
System.out.println("data.length: " + data.length + ".");
for (int i = 0; i < data.length; i++) {
System.out.println("data["+i+"].length: " + data[i].length + ".");
for (int j = 0; j < data[i].length; j++) {
System.out.println("data["+j+"]: " + data[i][j]);
}
}
}
}
}
Post a Comment