本文介绍了MATLAB:获取单元阵列从MATLAB到Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一些包含数组细胞类型的对象在创建数组双打。
这基本上是一个< 1XN细胞>数组,每个单元是双打的数组

I have an array created in MATLAB that contains a number of cell type objects which contain arrays of doubles.It's basically a <1xn cell> array and each cell is an array of doubles.

我想要做的就是以某种方式导出这些,这样我就可以将数据插入Java作为int类型的数组一个衣衫褴褛的数组。如何最好地做到这一点任何想法?

What I want to do is to somehow export these so that I can then insert the data into Java as a ragged array of arrays of type int. Any thought on how to best do this?

推荐答案

这是很难建立在Matlab原语的Java数组,因为Matlab的希望autobox它放回一个Matlab阵列。

It's difficult to construct a Java array of primitives in Matlab, because Matlab wants to autobox it back into a Matlab array.

你可以做的是创建一个Java类来帮助你,使用方法签名来指导Matlab的自动装箱。像这样的包装层可能比通过文本导出往返更加快捷,方便。

What you can do is create a Java class to help you, using the method signatures to guide Matlab's autoboxing. A wrapper layer like this may be faster and more convenient than a round trip through a text export.

package test;

/**
 * Class to help build Java arrays from Matlab.
 */
public class JavaArrayBuilder {
    /**
     * Assign an array into a larger ragged array
     * @param array ragged array you're building
     * @param i index into array
     * @param subarray this gets autoboxed to int[] from Matlab
     */
    public static void assignIntArray(Object[] array, int i, int[] subarray) {
        array[i] = subarray;
    }
}

然后就可以从MATLAB这样调用它。

Then you can call it from Matlab like this.

function ja = build_int_array

mynums = { 1:2, 1:5, 1:7 };
% Create a Java array of arrays
dummy = java.lang.Object();
ja = java.lang.reflect.Array.newInstance(dummy.getClass(), numel(mynums));
for i = 1:numel(mynums)
    test.JavaArrayBuilder.assignIntArray(ja, i-1, mynums{i});
end
% Now you have a Java ragged array, albeit as Object[] instead of int[][]

之后,您需要将对象[]数组转换为Java内部INT [] [],因为Matlab的拆箱会以Java int [] []回Matlab的数组。保持它作为M- code范围内的对象[]保护它。

Afterwards, you'll need to convert the Object[] array to int[][] within Java, because Matlab will unbox a Java int[][] back to a Matlab array. Keeping it as Object[] within M-code protects it.

您也可以建立使用类似包装列表或其他集合。这可能与其他Java code网更好,收藏不要在Matlab拆箱。

You could also build a List or other Collection using similar wrappers. That might mesh better with your other Java code, and Collections don't unbox in Matlab.

这篇关于MATLAB:获取单元阵列从MATLAB到Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 03:59