简体   繁体   English

在MatLab中将Java方法参数调用到数组中

[英]calling Java method arguments into array in MatLab

After creating the java class path for JAR file in MatLab. 在MatLab中为JAR文件创建Java类路径之后。 I am calling the java method that return argument for every 1 sec of type double. 我正在调用java方法,该方法每隔1秒类型类型double返回一个参数。 How to save this return arguments into the Array ? 如何将此返回参数保存到Array中?

import com.IPConn;
import com.V2;             % Java Class from JAR file
import java.util.ArrayList;

al = handle(V2(UI, ipcon), 'CallbackProperties'); % creating device Object which is the hardware(micro controller) sends the data
set(al,'callback',@(handles,event) event.getsource);
al.period(1000);           % This gives the event.getsource for every 1 sec

Is there any way to store this recurring values of getsource in to the array for example A=[250;500;....] by using Java Array List import or some other functions? 是否有任何方法可以通过使用Java数组列表导入或其他一些功能将getsource的此重复值存储到数组中,例如A = [250; 500; ....]?

If I understand correctly from your question, you have a java method which is returning an int. 如果我从您的问题中正确理解,您有一个返回int的java方法。 And this java method is getting called multiple times from Matlab and you want to store it's output in an array in Matlab. 而且从Matlab多次调用了此java方法,您希望将其输出存储在Matlab中的数组中。

Here is how you can do it: 这是您可以执行的操作:

Java code: Java代码:

import java.util.Random;
public class StackOverflow {
    public static int getRandomInt() {
        int max = 10;
        int min = 1;
        Random random = new Random();
        return random.nextInt(max - min + 1) + min;
    }
}

Build this into a jar and invoke it in Matlab as follows: 将其构建到一个jar中,然后在Matlab中调用它,如下所示:

javaclasspath('/full/path/to/your.jar')
import StackOverflow
so = StackOverflow;
A = zeros(1,5);
for i=1:5
    A(i) = so.getRandomInt;
end

The resultant variable A will have your desired array. 结果变量A将具有所需的数组。

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

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