从Java类型回到MATLAB类型 [英] Moving from Java types back to MATLAB types
问题描述
我在MATLAB工作区中有一个Java数组(在下面的示例中为my_array
),我想将其转换回MATLAB数组.
I have a Java array (my_array
in the example below) in my MATLAB workspace that I would like to convert back to a MATLAB array.
whos my_array
Name Size Class
my_array 20000x1 java.lang.Object[]
我在外部接口文档中找不到如何将Java类型转换回MATLAB类型的方法.我发现的唯一内容是此(有关MATLAB单元的文档),其中说我可以使用MATLAB单元即可完成此任务.
I could not find how to convert Java types back to MATLAB types in the External Interfaces documentation. The only thing I found is this (the documentation about MATLAB cells) which says that I can use MATLAB cells to do this task.
使用MATLAB cells
似乎是一个矫kill过正,特别是当我处理成千上万个相同类型的元素(在本例中为double
)时.还有其他方法可以将Java对象移回MATLAB的本机类型吗?
Using MATLAB cells
seems an overkill, specially when I am handling thousands of elements of the same type (in this case, double
). Is there any other way of moving Java objects back into MATLAB's native types?
推荐答案
示例:
%# example Object[] array
my_array = javaArray('java.lang.Object', 5);
for i=1:5, my_array(i) = java.lang.Double(i); end
%# convert to MATLAB vector
M = arrayfun(@(x)x, my_array);
%# or equivalently
M = cell2mat( cell(my_array) );
>> whos M
Name Size Bytes Class Attributes
M 5x1 40 double
这篇关于从Java类型回到MATLAB类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!