简体   繁体   中英

calling Java method arguments into array in MatLab

After creating the java class path for JAR file in MatLab. I am calling the java method that return argument for every 1 sec of type double. How to save this return arguments into the 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?

If I understand correctly from your question, you have a java method which is returning an int. And this java method is getting called multiple times from Matlab and you want to store it's output in an array in Matlab.

Here is how you can do it:

Java code:

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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