# Combining matlab and java

sumedha vangury

Greenhorn

Posts: 29

posted 4 years ago

Hi,i am making a project on digital image watermaring in java.I have found a matlab code on the internet that is better than the code i have implemented in my project.I wanted to ask is there a way where i can embed the entire matlab code in my java program?Is there any method with which this can be made possible?Thanyou.

Sumedha.

Sumedha.

posted 4 years ago

It sure looks like it to me. When I used the web search keywords matlab java I got a list which included several products which claimed to allow Java to use Matlab.

sumedha vangury

Greenhorn

Posts: 29

posted 4 years ago

I am getting a lot of errors when i try to run the following code given here

Please can you help me in solving,i really dont know what to do and i am soo confused.Please help me out.

Please can you help me in solving,i really dont know what to do and i am soo confused.Please help me out.

posted 4 years ago

I've been doing a fair amount of Java and Matlab.

I know it's fairly easy to call Java from Matlab but I don't know of an easy way to call a Matlab routine from Java.

Is your application a Java app or a Matlab app?

Joe

I know it's fairly easy to call Java from Matlab but I don't know of an easy way to call a Matlab routine from Java.

Is your application a Java app or a Matlab app?

Joe

It's not what your program can do, it's what your users do with the program.

sumedha vangury

Greenhorn

Posts: 29

posted 4 years ago

Hi Joe thanks for your reply,I am working on a java application and facing many problems in connecting matlab and java.I have given a detailed explanation of my problem in this post webpage

Please help me out if possible.I am really confused but i need to do it as soon as possible.Thankyou.

Please help me out if possible.I am really confused but i need to do it as soon as possible.Thankyou.

posted 4 years ago

Sumedha,

I admit that I have not experience with that package (matlabconrol). I rejected that approach for several reasons when I looked at this problem. That doesn't mean you should reject it also, just that I have no experience with the particular package.

To step back and look at the problem, if I understand it correctly you have a matlab function that works well and a Java program that does everything but that function working well. How to combine them?

The simplest to implement in my mind would be to have the Java code write a file with the input parmeters, call a Matlab program that read the file called the function and wrote the return value(s) to another file. It could all be done under the covers so the UI is seamless but launching Matlab is expensive, it won't be quick. But it might be acceptable if you can queue up a lot of work for Matlab to do once it's started, or if this the whole thing is a background task that the user doesn't sit and wait for.

There is a Coder toolbox that converts Matlab code to C/C++ that you can call as a function or a program. I've just started looking into it for much the same reason you have. It's not as clean as here's my Matlab routines make me a dynamic library I can write a simple JNI interface to. I'm not sure how much work it takes to convert my Matlab code into something compatible. Nor do I have a handle on how much overhead it is.

An easier Matlab centric approach is to have the user run a Matlab program and package what Java code you have as classes that can be called from Matlab. It's not that bad unless you are passing complicated data structures back and forth. Even then it's conceptually simple just a lot of work. The big problem I have with this approach is Matlab's GUI (Guide) is not as nice as Java's. That's the GUI the user's see not the IDE.

That class library you linked to is one of a few that I've seen that implement this approach. I have no real objection to the architecture in concept but I haven't found a compelling reason to do it.

What this approach does is start up a Matlab program that listens on a socket for Matlab statement that it then interprets and sends back the result.

As I said I have no experience with the package you chose but the error message you linked to suggests to me that the Matlab side of things is not running when the Java side is trying to connect. I don't know if the library is supposed to start the Matlab process as needed, if the Matlab side runs as a service, or if you're supposed to do that manually. If it's supposed to do it automatically or as a service and you have Matlab running in another window you may be seeing a licensing issue. It's not very clear to me when you can and cannot run multiple instances of Matlab on a single user license.

When you get it running I would be very interested in your impression of how effective it is. Sorry I couldn't answer your question directly.

Joe

sumedha yadav wrote:Hi Joe thanks for your reply,I am working on a java application and facing many problems in connecting matlab and java.I have given a detailed explanation of my problem in this post webpage

Please help me out if possible.I am really confused but i need to do it as soon as possible.Thankyou.

Sumedha,

I admit that I have not experience with that package (matlabconrol). I rejected that approach for several reasons when I looked at this problem. That doesn't mean you should reject it also, just that I have no experience with the particular package.

To step back and look at the problem, if I understand it correctly you have a matlab function that works well and a Java program that does everything but that function working well. How to combine them?

**The get something working today option:**

The simplest to implement in my mind would be to have the Java code write a file with the input parmeters, call a Matlab program that read the file called the function and wrote the return value(s) to another file. It could all be done under the covers so the UI is seamless but launching Matlab is expensive, it won't be quick. But it might be acceptable if you can queue up a lot of work for Matlab to do once it's started, or if this the whole thing is a background task that the user doesn't sit and wait for.

**The do it the Matlab way options:**

There is a Coder toolbox that converts Matlab code to C/C++ that you can call as a function or a program. I've just started looking into it for much the same reason you have. It's not as clean as here's my Matlab routines make me a dynamic library I can write a simple JNI interface to. I'm not sure how much work it takes to convert my Matlab code into something compatible. Nor do I have a handle on how much overhead it is.

An easier Matlab centric approach is to have the user run a Matlab program and package what Java code you have as classes that can be called from Matlab. It's not that bad unless you are passing complicated data structures back and forth. Even then it's conceptually simple just a lot of work. The big problem I have with this approach is Matlab's GUI (Guide) is not as nice as Java's. That's the GUI the user's see not the IDE.

**The proprietary RPC**

That class library you linked to is one of a few that I've seen that implement this approach. I have no real objection to the architecture in concept but I haven't found a compelling reason to do it.

What this approach does is start up a Matlab program that listens on a socket for Matlab statement that it then interprets and sends back the result.

As I said I have no experience with the package you chose but the error message you linked to suggests to me that the Matlab side of things is not running when the Java side is trying to connect. I don't know if the library is supposed to start the Matlab process as needed, if the Matlab side runs as a service, or if you're supposed to do that manually. If it's supposed to do it automatically or as a service and you have Matlab running in another window you may be seeing a licensing issue. It's not very clear to me when you can and cannot run multiple instances of Matlab on a single user license.

When you get it running I would be very interested in your impression of how effective it is. Sorry I couldn't answer your question directly.

Joe

It's not what your program can do, it's what your users do with the program.

sumedha vangury

Greenhorn

Posts: 29

posted 4 years ago

Joe,thanks a lot for taking out your valuable time to reply back to my post

yes you got it right,thats exactly what my problem says.

Basically,its my java project in digital image watermarking that has three algorithms fully coded in java,the fourth algorithm is in matlab so thats the reason i want to connect java to matlab,instead of converting the entire code to java.

Running java from matlab,that is,actually doing it the other way round may seem easier but then i would have to convert my entire project from java to malab and more over it would require me to sit and understand and study everything about how to make it work and it may actually be breadth taking for me to put in soo much of efforts all over again.

I would most probably like to stick to the current approach,if nothing works though i will do it using matlab with java gui.

I am actually going through it over and over again and looking out for ways to fix it,if i get the thing running,i will definitely let you know the way i did it.Thankyou.

To step back and look at the problem, if I understand it correctly you have a matlab function that works well and a Java program that does everything but that function working well. How to combine them?

yes you got it right,thats exactly what my problem says.

The get something working today option:

The simplest to implement in my mind would be to have the Java code write a file with the input parmeters, call a Matlab program that read the file called the function and wrote the return value(s) to another file. It could all be done under the covers so the UI is seamless but launching Matlab is expensive, it won't be quick. But it might be acceptable if you can queue up a lot of work for Matlab to do once it's started, or if this the whole thing is a background task that the user doesn't sit and wait for.

Basically,its my java project in digital image watermarking that has three algorithms fully coded in java,the fourth algorithm is in matlab so thats the reason i want to connect java to matlab,instead of converting the entire code to java.

The do it the Matlab way options:

There is a Coder toolbox that converts Matlab code to C/C++ that you can call as a function or a program. I've just started looking into it for much the same reason you have. It's not as clean as here's my Matlab routines make me a dynamic library I can write a simple JNI interface to. I'm not sure how much work it takes to convert my Matlab code into something compatible. Nor do I have a handle on how much overhead it is.

An easier Matlab centric approach is to have the user run a Matlab program and package what Java code you have as classes that can be called from Matlab. It's not that bad unless you are passing complicated data structures back and forth. Even then it's conceptually simple just a lot of work. The big problem I have with this approach is Matlab's GUI (Guide) is not as nice as Java's. That's the GUI the user's see not the IDE.

Running java from matlab,that is,actually doing it the other way round may seem easier but then i would have to convert my entire project from java to malab and more over it would require me to sit and understand and study everything about how to make it work and it may actually be breadth taking for me to put in soo much of efforts all over again.

I would most probably like to stick to the current approach,if nothing works though i will do it using matlab with java gui.

I am actually going through it over and over again and looking out for ways to fix it,if i get the thing running,i will definitely let you know the way i did it.Thankyou.

posted 4 years ago

Sumedha,

It's not quite that bad. It's not trivial but in Matlab you can call Java classes from jar files with code that looks like:

Your java code can have it's own GUI although there's some details in making sure it is in the Matlab event loop. And you do have to add the appropriate entries to Matlab's javaclasspath (see Matlab's javaaddpath function).

I have several packages that work both from the command line and inside a Matlab program. The trick there is that the Main method in the primary class sets up and starts the Java GUI. Matlab instantiates the Class and uses other methods to run the GUI and still other methods to extract the data it needs.

I am not suggesting to rewrite you code just discussing an alternate way that

Good luck,

Joe

Running java from matlab,that is,actually doing it the other way round may seem easier but then i would have to convert my entire project from java to malab and more over it would require me to sit and understand and study everything about how to make it work and it may actually be breadth taking for me to put in soo much of efforts all over again.

Sumedha,

It's not quite that bad. It's not trivial but in Matlab you can call Java classes from jar files with code that looks like:

Your java code can have it's own GUI although there's some details in making sure it is in the Matlab event loop. And you do have to add the appropriate entries to Matlab's javaclasspath (see Matlab's javaaddpath function).

I have several packages that work both from the command line and inside a Matlab program. The trick there is that the Main method in the primary class sets up and starts the Java GUI. Matlab instantiates the Class and uses other methods to run the GUI and still other methods to extract the data it needs.

I am not suggesting to rewrite you code just discussing an alternate way that

**might**be helpful.

Good luck,

Joe

It's not what your program can do, it's what your users do with the program.

sumedha vangury

Greenhorn

Posts: 29

posted 4 years ago

Hi,so after long struggle i sorted out my problem.I was previously running the matlab control jar file on my desktop which gave me the error

After a lot of google search on the error i concluded that the error was because there were multiple versions of matlab installed on my desktop.Even if you uninstall all versions and keep just one you still get the error because there are some registry keys that do not get deleted and hence leave some traces of matlab behind,you need another software if you want to entirely cleanup matlab.

So basically there was a conflict in the location path hence the matlab control jar file could not detect as to which location path it should use to launch matlab.

I actually couldn't waste much time in solving the error hence a quick fix was to use my laptop in which i had never installed matlab.Now its working perfectly and i am still working on the rest of the code that i was supposed to do.

I am deliberately posting the detailed explanation so that someone facing a similar problem as mine may end up on this page and magically find the sollution;)

Thankyou

After a lot of google search on the error i concluded that the error was because there were multiple versions of matlab installed on my desktop.Even if you uninstall all versions and keep just one you still get the error because there are some registry keys that do not get deleted and hence leave some traces of matlab behind,you need another software if you want to entirely cleanup matlab.

So basically there was a conflict in the location path hence the matlab control jar file could not detect as to which location path it should use to launch matlab.

I actually couldn't waste much time in solving the error hence a quick fix was to use my laptop in which i had never installed matlab.Now its working perfectly and i am still working on the rest of the code that i was supposed to do.

I am deliberately posting the detailed explanation so that someone facing a similar problem as mine may end up on this page and magically find the sollution;)

Thankyou