9

I am trying to call a user defined Matlab Function(M file) which takes 3 arguments(Java Strings) from my Java application which is developed in Eclipse. At the moment I am able to call proxy.eval and proxy.feval methods with the functions/commands like disp or sqr. But when i try to invoke a user-defined function it says on the matlab console that there is no such function defined like that and on the Java console MatlabInvocationException occurs.

Then I tried with a simple user-defined function which takes no arguments and just has single line disp('Hello') but still the result is same. So I think rather than a type conversion problem there is something wrong with how user-defined functions are getting invoked.

Please can anyone help me soon? I am meeting the deadline very soon for this project. I would be so thankful if someone can come up with a solution. (Mr Joshuwa Kaplan, is there any guide on solving an issue like this in your posts? I tried but found nothing)

Thanks in advance

2
  • To make the question clear, the OP is using matlabcontrol, an API to call MATLAB from Java programs (relies upon the undocumented JMI)
    – Amro
    Oct 9, 2011 at 22:15
  • Just FYI, matconsolectl is an actively-maintained fork of matlabcontrol.
    – Ned Twigg
    Oct 16, 2015 at 23:46

1 Answer 1

11

You must have any user-defined m-files on the MATLAB search path, just as if you were working normally inside MATLAB.

I tested with the following example:

C:\some\path\myfunc.m

function myfunc()
    disp('hello from MYFUNC')
end

HelloWorld.java

import matlabcontrol.*;

public class HelloWorld
{
    public static void main(String[] args)
        throws MatlabConnectionException, MatlabInvocationException
    {
         // create proxy
         MatlabProxyFactoryOptions options =
            new MatlabProxyFactoryOptions.Builder()
                .setUsePreviouslyControlledSession(true)
                .build();
        MatlabProxyFactory factory = new MatlabProxyFactory(options);
        MatlabProxy proxy = factory.getProxy();

        // call builtin function
        proxy.eval("disp('hello world')");

        // call user-defined function (must be on the path)
        proxy.eval("addpath('C:\\some\\path')");
        proxy.feval("myfunc");
        proxy.eval("rmpath('C:\\some\\path')");

        // close connection
        proxy.disconnect();
    }
}

We compile and run the Java program:

javac -cp matlabcontrol-4.0.0.jar HelloWorld.java
java -cp ".;matlabcontrol-4.0.0.jar" HelloWorld

a MATLAB session will open up, and display the output:

hello world
hello from MYFUNC

You could also add your folder to the path once, then persist it using SAVEPATH. That way you won't have to do it each time.

1
  • 1
    It was really quick and accurate. I had to remove the proxy.eval("rmpath('C:\\some\\path')"); as in my code, i think that is because the of the computation i have to do in matlab and it takes a little bit of time. And i am very much thankful to you. i tried this more than 2 days continuously. so this is a very big help for me. once again Thank you very much and appreciate your effort in this.Cheers Amro :) :):) Aug 27, 2011 at 6:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.