When used Matlabelengine to execute the same function from Java, I obtained the same result

matlab = MatlabEngine.startMatlab(options);
matlab.eval("data = "+ dto.getName()+"('"+dto.getId()+"')");
String data = matlab.getVariable("data");
function data = getMyData(id)
data = jsonencode(struct("da",id))

4 Comments

mark : Multiple executions the matlab funciton
It's not clear to me:
  • What the problem is here. Unless you're using random data (generated with rand, randn, randi, etc.) it seems like running the same program multiple times and getting the same answer each time is a good thing! Nothing in your code shows that you're using any random numbers.
  • What options you're using when you startMatlab.
  • What the dto variable that you use in your matlab.eval call represents. There's no function by that name in MATLAB.
which -all dto
'dto' not found.
  • Where the getMyData function is being called.
Firstly, my MATLAB script is a function that randomly or fetches data, and there is no problemhis function.
When I execute with tn MATLAB, the output results are random.
But when I use Matlabengine eval in Java, the result obtained from my first execution . there are no changes.
so is this a bug inmatlabengine when i use matlabengine to execute matlab function , i got the same results or
Is there a way to solve this problem
  • my function
matlab script filename 'myTest.m'
function data = getMyData(id)
%do something
%get the random result dataStruct
data = jsonencode(struct("da",dataStruct))
end
java method
JSONObject obj =new JSONObject();
String command = "myTest('10000')";
MatlabEngine matlab=matlab = MatlabEngine.startMatlab(options);
matlab.eval(command);
matlab.close();
when i use matlabengine to obtain multiple execution results, the results are the same every time

Sign in to comment.

 Accepted Answer

I tried to start Matlabengine through springboot to ensure that there is only one Matlabengine bean for each springboot startup project

More Answers (0)

Products

Release

R2023a

Asked:

on 18 Aug 2023

Edited:

on 8 Sep 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!