If a Java®方法返回原始数据类型,然后返回MATLAB®converts the data, as shown in the table in原始返回类型。
如果Java方法签名指定类型的返回数据java.lang.Object
,然后matlab转换返回的实际类型,如表中所示java.lang.Object返回类型。
MATLABdoes not convert other Java objects to MATLAB types. For information about handling this data, see将Java对象转换为MATLAB类型的功能。
MATLABconverts primitive data returned from a Java method into types that best represent the data to the MATLAB language. This table shows how MATLAB converts the data. For some Java types, MATLAB treats scalar and array returns differently.
Java返回类型 | 产生的MATLAB类型 - 标量 | Resulting MATLAB Type — Array |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The signature for thejava.lang.String
方法Tochararray
是:
公共char [] tochararray()
调用该方法细绳
目的。MATLAB将输出转换为char
大批。
str = java.lang.String('你好');res = str.toCharArray'
res = 1×5 char数组你好
java.lang.Object
Return TypesWhen a Java method is declared to return data of typejava.lang.Object
,,,,MATLABconverts its value depending on the actual type returned. This table shows how MATLAB converts the data.
Actual Java Type | 产生的MATLAB类型 - 标量 |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
如果返回参数是一个子类,则没有转换Object
or an array ofObject
。The object remains a Java object. However, if you index into a returnedObject
数组,MATLAB根据表转换值。有关更多信息,请参阅将对象数组元素转换为MATLAB类型。
Refer to the following signature for agetData
方法。
java.lang.Object getData()
IfgetData
返回ajava.lang.integer
对象,然后matlab将值转换为双倍的
。
MATLABonly converts object data return values if the method signature specifiesjava.lang.Object
。如果签名指定任何其他对象类型,则MATLAB不会转换值。例如,MATLAB确实转换了此方法签名的返回值:
java.lang.Object getData()
但是MATLAB不会转换此方法的返回值:
java.lang.string getData()
To convert Java object data to MATLAB data, use MATLAB functions as described in these topics:
To convert Java numeric types to MATLAB types, use a numeric MATLAB function like双倍的
。The action taken by the双倍的
函数取决于您指定的对象的类。
如果对象是从java.lang.Number
,然后MATLAB将对象转换为MATLAB双倍的
。
如果对象不是数字类的实例,则MATLAB检查一个类定义todouble
方法。MATLABcalls this method to perform the conversion.
If you create your own class, then write atodouble
指定自己的类型转换的方法。
笔记
如果对象的类不是derived fromjava.lang.Number
and it does not implement atodouble
方法,,,,then the双倍的
功能显示错误消息。
To convertjava.lang.String
objects and arrays to MATLAB strings or character vectors, use the MATLAB细绳
orchar
功能。
If the object specified in the MATLAB function is not an instance of thejava.lang.String
class, then MATLAB checks its class definition for ato
or aTochar
方法。If you create your own class, then write ato
orTochar
方法to specify the string conversion.
笔记
如果对象的类不是java.lang.String
and it does not implement aTochar
方法,,,,then thechar
功能显示错误消息。
如果Java类定义了字段名称,请使用结构
功能将对象数据转换为MATLAB结构。
Suppose that you call a Java method that returns ajava.awt.polygon
目的。该类定义字段XPoints
andypoints
。要运行此示例,请创建一个polygon
多变的。
polygon = java.awt.polygon([14 42 98 124],[55 12 -2 62],4);
将对象转换为结构并显示第三点的X,y坐标。
psstruct = struct(polygon)
ps struct =带字段的结构:npoints:4 xpoints:[4×1 int32] ypoints:[4×1 int32]
如果您的Java方法返回不同类型的数据,请使用cell
功能可将数据转换为MATLAB类型。根据所得单元阵列的元素根据原始返回类型andjava.lang.Object返回类型表。
假设您调用返回类型参数的Java方法java.lang.Double
,,,,java.awt.Point
,,,,andjava.lang.String
。To run this example, create variables of these types.
进口java.lang。*java.aw。*% Create a Java array of doubledblarray = javaarray('java.lang.Double',1,10);为了m = 1:10 dblarray(1,m)= double(m * 7);结尾%创建Java点数ptarray = javaarray('java.awt.point',3);ptarray(1)=点(7.1,22);ptarray(2)=点(5.2,35);ptarray(3)=点(3.1,49);% Create a Java array of stringsstrArray = javaArray('java.lang.String',2,2);Strarray(1,1)=字符串('one');Strarray(1,2)=字符串('二');Strarray(2,1)=字符串('three');Strarray(2,2)=字符串('four');
将每个数组转换为单元格数组。您可以使用cellArray
在MATLAB函数中。
cellArray = {cell(dblArray),cell(ptArray),cell(strArray)}
cellarray = 1×3个单元格数组{1×10个小区} {3×1个蜂窝} {2×2 cell}
每个单元格保持不同类型的数组。显示内容。
cellarray {1,1}%double类型阵列
ANS = 1×10个单元格阵列[7] [14] [21] [28] [35] [42] [49] [56] [63] [70]
cellarray {1,2}% Array of type Java.awt.Point
ans = 3×1单元格数[1×1 Java.awt.point] [1×1 Java.awt.point] [1×1 Java.aw.awt.point]
cellarray {1,3}% Array of type char array
ans = 2×2单元阵列的一个“两个”“三”“四”