从MATLAB传递到Java的奇怪类 [英] Strange classes passed from MATLAB to Java

查看:77
本文介绍了从MATLAB传递到Java的奇怪类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些需要互相交谈的MATLAB代码和一些Java代码.我得到一个NoSuchMethodError.当我将MATLAB双精度数组传递给接受double[]参数的Java方法时.

I have some MATLAB code and some Java code that need to talk with each other. I was getting a NoSuchMethodError. When I pass a MATLAB double array to a Java method that accepts double[] argument.

所以我写了一个简单的"hello world"来获取传递给方法的对象的类

So I write a simple "hello world" to get the class of an object passed to the method

public void printArray(Object array) {

        System.out.println(array.getClass());
        System.out.println(array.getClass().getPackage());
    }

从MATLAB调用此方法,得到以下有趣的输出:

Calling this method from MATLAB, I get this interesting output:

>> a.printArray(2)
class java.lang.Double
package java.lang
>> a.printArray('hello')
class java.lang.String
package java.lang
>> a.printArray(true)
class java.lang.Boolean
package java.lang
>> a.printArray([2 3 4])
class [D
null
>> a.printArray([true false])
class [Z
null

有人可以解释发生了什么.我有MATLAB R14,并且Java类的编译兼容性为1.3.

Can someone explain whats happening. I have MATLAB R14 and the Java class is compiled with 1.3 compatibility.

推荐答案

我认为最初的问题是

I think the original problem has been updated by the OP, so I'll take the chance to summarize our findings so far:

  • We have established that the sample code in the original question produces the expected behavior. MATLAB passes data as primitives to Java, and Java performs the appropriate autoboxing to Objects. As pointed out in Matthew Simoneau's reply, MATLAB explains how it matches its data types to Java data types in the "Passing Data to a Java Method" section of its documentation. The surprising thing is that a single MATLAB data type may match different Java data types, e.g. logical matches boolean, byte, short, int, long, float, and double, in that order of precedence.

OP最初遇到的NoSuchMethodError是由使用错误的方法引起的.这是不再是问题. 使用double[]作为方法参数有效.

The NoSuchMethodError that the OP initially encountered was caused by the use of a wrong method. This is no longer a problem. Using double[] as the method argument works.

奇怪的"类名称([D[Z)实际上是Java用于描述基本类型数组的符号. API解释了 Class.getName().

The "strange" class names ([D and [Z) are actually notations used by Java to describe arrays of primitive types. The API explains the usage in Class.getName().

关闭案例=)

这篇关于从MATLAB传递到Java的奇怪类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆