I think Matlab is often needed for OR people, for solving equations, taking derivatives, etc.. Although I like mathematical software, I am using Object Oriented Programming instead, since I’m more familiar to them. However due to limitations of programming languages, we may need to use math software.
For my thesis coding I’m using Java. I need to solve an equation for my heuristic. I will go through this case. The equation I need to solve was;
There is a library “matlabcontrol” on the internet for connecting Matlab to Java. It allows to use Matlab commands (setting/getting variables, solving equations, returning operations). You can reach library here, and download here.
So, how could we use it? First of all import library to your project. If you are using eclipse, you can find how to import libraries from this link or video. After importing, we are ready to go.
MatlabProxyFactory factory = new MatlabProxyFactory(); MatlabProxy proxy = factory.getProxy();
This command will open a Matlab screen. Then for my case, I solve the equation by the following code;
String f = 'fsolve(@(a)integral(@(t)(10-t),0,a)-9,0)'; Object d = proxy.returningEval(f,1);
Here returningEval function run the command and return the output of the Matlab. fsolve is used to solve equation. It gives the result, where the function (first parameter) is zero. Therefore, I move “9” to left with minus sign.
Resulting array is outputs. What we need is the value of the “a”. So, using following codes will help me;
Object arg = d; double result = ((double) arg);
We did it! Finally we have the value of the “a”.
In this post, I tried to show a very simple way to connect Matlab to your Java code. If you have questions, I would be happy to answer.
(Photo by Marc Amos)
Easiest Way to Use Matlab over Java « OR Complete | Collective Operations Research Blog http://t.co/RwkfMne5 coding tips
Jython! http://t.co/xPGkPdp2 > RT @parubin: Easiest Way to Use Matlab over Java « @orcomplete: http://t.co/5W675xmW coding tips
RT @or_exchange: Jython! http://t.co/Q44NPSTD RT @parubin: Easiest Way to Use Matlab over Java « @orcomplete: http://t.co/hNwL7DoX
Thanks for sharing this tip — wish I had known this when I was doing my thesis.
Just to comment on your first paragraph; I also had to go from Matlab to Java, which I didn’t like, for the reasons mentioned here: http://kuomarc.wordpress.com/2012/01/27/why-i-love-common-lisp-and-hate-java/
I have since moved to Lisp. Here’s a post on why I think Lisp might be a good language to learn for OR. http://kuomarc.wordpress.com/2012/03/05/the-uncommon-lisp-approach-to-operations-research/
Thanks Marc for your comment Actually I haven’t heard of Lisp, my reaction at first is the same “common what?”
As far as I see, your posts are so helpful about coding for OR. I would like to explore more, and also learn about Lisp, as you advertise so much.
Hope to see your posts in here, too. We would like to hear about Lisp from you.
Easiest Way to Use Matlab over Java « OR Complete | Collective Operations Research Blog http://t.co/L53jbaDr #orms #orblog via @orcomplete
I am using MatlabProxyFactory for calling a function using returningFeval(‘MyMatlabFunc.m’, 1, args).
I have to call that matlab function multiple times in a loop.
Every time that function is called matlab window is opened.
Is there a way that I can suppress matlab window opening?
How can I call matlab function in background without java user knowing that matlab is being called?
If you use same MatlabProxy object, then it doesn’t open a new MATLAB window, it uses the same window. To open a hidden process, check the following answer at StackOverflow:
I have not tried it yet, but it seems .setHidden(true) is the option you want. But I suggest you to solve multiple window problem first, and then you can try to hide it.