简体   繁体   English

如何运行Matlab编译的Java函数?

[英]How to run Matlab compiled Java function?

I have compiled a Matlab script to Java with Matlab compiler SDK. 我用Matlab编译器SDK编译了一个Matlab脚本到Java。 Everything worked fine. 一切都很好。 Matlab generated a Jar.file and i included it into my Java Project in eclipse. Matlab生成了一个Jar.file,我把它包含在eclipse中的Java Project中。 Now my Problem is, the matlab script contains a complex algorithm function. 现在我的问题是,matlab脚本包含一个复杂的算法函数。 This function can be now called in Java. 现在可以在Java中调用此函数。 I need to read 10 csv files with each containing 10.000 rows with 4 columns of data and pass now the same arguments to the java function as i did in matlab. 我需要读取10个csv文件,每个文件包含带有4列数据的10.000行,现在将相同的参数传递给我在matlab中的java函数。

The way my csv files are: 4 columns, 10.000 rows. 我的csv文件的方式是:4列,10.000行。

a x y z
1 3 4 5
4 4 5 6
. . . .

readsfirst the data in a seperate function in the variables.Also get length of a. readfirst数据在变量的单独函数中。也得到a的长度。

[a,x,y,z] = readData(['csv\' files(1).name]);
sizeOfa=length(a);

after i call my algorithm function 3 times with different columns and also pass the size of a. 在我用不同的列调用我的算法函数3次之后,也传递了a的大小。

   algorithm(a,x,sizeOfa);
   algorithm(a,y,sizeOfa);
   algorithm(a,z,sizeOfa);

after this the definition of my algorithm comes. 在此之后我的算法的定义来了。

function y= algorithm(x,y,sizeofX)
   do some stuff...
   end

Now my question: 现在我的问题:

my values read from csv file are stored in Matlab in a 10000*1 Matrix. 我从csv文件读取的值存储在Matlab中的10000 * 1矩阵中。 If i want to call the same function now in Java. 如果我想在Java中调用相同的函数。 What should i pass in to my function? 我应该把什么传递给我的功能? I can read my values a, x, y, z all in seperate arrays. 我可以在单独的数组中读取我的值a,x,y,z。 but an array is 1*10000. 但是一个数组是1 * 10000。 Can i just pass an array? 我可以通过一个阵列吗? The readfile function was not compiled to java, i just compiled my algorithm function. readfile函数没有编译成java,我只是编译了我的算法函数。

I found two solutions for my problem. 我为我的问题找到了两个解决方案。

MATLAB Solution MATLAB解决方案

My Matlab function used a 10000*1 Array. 我的Matlab函数使用10000 * 1数组。 It is a Matrix like this 像这样的矩阵

1
2
3
4
..
10000

An Java array passed to compiled function would be different. 传递给已编译函数的Java数组会有所不同。 It looks like this 看起来像这样

1 2 3 4 .. 10000

Now what did to solve this was, i tranponse the arguments passed to my matlab function. 现在解决这个问题的是,我转发了传递给我的matlab函数的参数。

   function y = algorithm(a,z,sizeOfa)
    {
      a=a'; // transpose 
      z=z'; //transpose
       ...
         //your stuff

    }

Now if i pass my array into my java function, the array is internally transpose to a matrix. 现在,如果我将我的数组传递给我的java函数,那么数组将在内部转置为矩阵。

EDIT: Better Solution found 编辑:找到更好的解决方案

Java Solution Java解决方案

double[] t;  // your Double Array

int tSize= t.length; // get Size

MWNumericArray result;

result= MWNumericArray.newInstance(new int[]{tSize,1},t,MWClassID.DOUBLE);

Pass result to your function, it is converted to an*1 Array, where n is the size of your array. 将结果传递给您的函数,它将转换为* 1数组,其中n是数组的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM