java解析MAT文件

    <dependency>
        <groupId>javax.media</groupId>
        <artifactId>jai_core</artifactId>
        <version>1.1.3</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.media/jai_codec -->
    <dependency>
        <groupId>javax.media</groupId>
        <artifactId>jai_codec</artifactId>
        <version>1.1.3</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.media/jai_imageio -->
    <dependency>
        <groupId>javax.media</groupId>
        <artifactId>jai_imageio</artifactId>
        <version>1.1</version>
    </dependency>

    <dependency>
        <groupId>org.ujmp</groupId>
        <artifactId>ujmp-jmatio</artifactId>
        <version>0.3.0</version>
    </dependency>

    <dependency>
        <groupId>us.hebi.matlab.mat</groupId>
        <artifactId>mfl-core</artifactId>
        <version>0.5.2</version>
    </dependency>
    <dependency>
        <groupId>us.hebi.matlab.mat</groupId>
        <artifactId>mfl-ejml</artifactId>
        <version>0.5.2</version>
    </dependency>

public class Mat5Util {
private static JSONObject analyMat(String filePath) {
JSONObject jsb = new JSONObject();
Source source = null;
try {
source = Sources.openFile(filePath);
MatFile mat = Mat5.newReader(source).readMat();
mat.getEntries().forEach(entry -> jsb.put(entry.getName(), struct2json(entry.getValue())));
// source.close();
} catch (Exception e) {
e.printStackTrace();
}finally {
if (source != null) {
try {
source.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
return jsb;
}

public static JSONArray analyMat2Array(String filePath) {
JSONObject temp = analyMat(filePath);
return temp.getJSONArray(temp.entrySet().iterator().next().getKey());

// return analyMat(filePath) .values().stream().limit(1).collect(Collectors.toCollection(JSONArray::new));
}
private static Object struct2json(Array array) {
MatlabType type = array.getType();
if (type == MatlabType.Structure) {
Struct struct = (Struct) array;
JSONObject jsonObject = new JSONObject();

List names = struct.getFieldNames();

// int m1 = struct.getNumRows();
int m = names.size();
int n = struct.getNumCols();
JSONArray ret = new JSONArray();
for (int j = 0; j < n; j++) {
JSONObject jsb1 = new JSONObject();
for (int i = 0; i < m; i++) {
jsb1.put(names.get(i), struct2json(struct.get(names.get(i), j)));
}
ret.add(jsb1);
}
return ret;
// for (int j=0;j<names.size();j++) {
// String name = names.get(j);
// jsonObject.put(name, struct2json(struct.get(name, 0,j)));
// }
// return jsonObject;
} else if (type == MatlabType.Cell) {
Cell cell = (Cell) array;
int m = cell.getNumRows();// 行
int n = cell.getNumCols();// 列

// if (m == 1 && n == 1) {
// return cell.get(0, 0);
// } else
if (m == 1 || n == 1) {
int temp = Math.max(m, n);
Object[] data = new Object[temp];
for (int i = 0; i < temp; i++) {
data[i] = struct2json(cell.get(i));
}
return data;
}
Object[][] data = new Object[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
data[i][j] = struct2json(cell.get(i, j));
}
return data;
} else if (type == MatlabType.Double) {
Matrix matrix = (Matrix) array;
int m = matrix.getNumRows();// 行
int n = matrix.getNumCols();// 列
// if (m == n && n == 1) {
// return matrix.getDouble(0, 0);
// } else
if (m == 1 || n == 1) {
int temp = Math.max(m, n);
double[] data1 = new double[temp];
for (int i = 0; i < temp; i++) {
data1[i] = matrix.getDouble(i);
}
return data1;
}
double[][] data = new double[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
data[i][j] = matrix.getDouble(i, j);
}
return data;

} else if (type == MatlabType.Int8 || type == MatlabType.Int16 || type == MatlabType.Int32 ||
type == MatlabType.Int64) {
Matrix matrix = (Matrix) array;

int m = matrix.getNumRows();// 行
int n = matrix.getNumCols();// 列

// if (m == n && n == 1) {
// return matrix.getLong(0, 0);
// } else
if (m == 1 || n == 1) {
int temp = Math.max(m, n);
long[] data = new long[temp];
for (int i = 0; i < temp; i++) {
data[i] = matrix.getLong(i);
}
return data;
}
long[][] data = new long[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
data[i][j] = matrix.getLong(i, j);
}
return data;
} else if (type == MatlabType.UInt8 || type == MatlabType.UInt16 || type == MatlabType.UInt32 ||
type == MatlabType.UInt64) {
return array.toString();
} else if (type == MatlabType.Object) {
return array.toString();
} else {
Char chr = (Char) array;

int m = chr.getNumRows();// 行
String[] data = new String[m];
for (int i = 0; i < m; i++) {
data[i] = chr.getRow(i);
}

// return m == 1 ? data[0] : data;
return data;
// JSONArray aa = new JSONArray();
// for (int i = 0; i < chr.getNumRows(); i++) {
// aa.add(chr.getRow(i));
// }
// return aa;
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值