如何在java中加载/打开/读取matlab文件* .mat?

时间:2015-04-20 13:26:05

标签: java matlab opencv mat mat-file

到目前为止我的代码(基于this entry),但它不起作用:

MatFileReader matfilereader = new MatFileReader("C:\\data\\Freebase\\initEmbed.mat");
System.out.println(matfilereader.getData().get(0) + "name: "+matfilereader.getMLArray("words").getSize());
System.out.println("new "+matfilereader.getMLArray("words"));
MLArray words = matfilereader.getMLArray("words");
System.out.println(words.contentToString().substring(0, 100));
MLChar j = (MLChar) matfilereader.getMLArray("words");

我的输出:

[1x91898  cell array]name: 91898
new [1x91898  cell array]
words = 
    [1x9  char array]   [1x3  char array]   [1x1  char array]   [1x1  char array]   [1x2  char array]   

Exception in thread "main" java.lang.ClassCastException: com.jmatio.types.MLCell cannot be cast to com.jmatio.types.MLChar
    at NTN.Run_NTN.main(Run_NTN.java:91)

如何将这个matlab文件的单词加载到java数组中?

1 个答案:

答案 0 :(得分:1)

我对matLab一无所知,但异常似乎很清楚。您正在向MLCell投射MLChar。 " getMLArray"必须返回MLCell。我会先加入MLCell

MLCell mlCell = (MLCell) matfilereader.getMLArray("words");

然后使用该类型可用的任何方法来获取MLChar