Importing Java Classes in Octave

后端 未结 1 1363
故里飘歌
故里飘歌 2021-01-20 09:27

I\'ve been having difficulty figuring out how to do this. From the Octave website, it seems that java classes are found via a class path. This Stack Overflow answer indica

相关标签:
1条回答
  • 2021-01-20 10:08

    The good news is, it is very easy to translate the java instructions from matlab syntax to octave syntax.

    The bad news is, you will have to translate the matlab syntax to octave syntax. While this is straightforward, it does mean you may have to hunt any java calls in the provided m-files as well (rather than just in your own code) and adapt the syntax. (Obviously you might come up with a nice way to automate the process instead.)

    Here is how I got the tutorial to work on octave:

    • I downloaded the matlab_examples zipfile and unzipped as instructed (I unzipped the folder on my desktop, i.e. on my machine this resulted in the folder /home/tasos/Desktop/matlab_examples
    • I open octave and cd into that directory
    • Open the load_javaplex.m file and remove all import statements, and then run it to "initialize" javaplex.
    • You are now ready to run the command api.Plex4.createExplicitSimplexStream() as indicated in the tutorial, BUT, first you need to note two things:

      1. Octave does not provide a way to import java classes from packages, therefore all your class calls need to be fully qualified by package. I.e. the Plex4 class of the api package will actually need to be fully qualified as edu.stanford.math.plex4.api.Plex4. You can confirm Plex4 is a class of the api package, which is itself a (sub)package of the edu.stanford.math.plex4 package by opening the .jar file and exploring its folder structure.

      2. The syntax for creating java objects, calling java methods, etc, is different in octave than in matlab. See the relevant page in the octave manual for details.

    Therefore the api.Plex4.createExplicitSimplexStream(), which is intended to call (with no arguments) the createExplicitSimplexStream method of the Plex4 class in the edu.stanford.math.plex4.api package, will be called in octave as follows:

    javaMethod( 'createExplicitSimplexStream', 'edu.stanford.math.plex4.api.Plex4')
    

    which then outputs as the answer desribed in the tutorial.

    Having said all that, note that, while you cannot import classes or (sub)packages directly to save you from having to rewrite long package strings all the time, octave's java interface does seem to rely on strings a lot, which means it is fairly easy to store such long strings as variables and reuse them at the point of having to access a class. So, e.g. you could save the string 'edu.stanford.math.plex4.' to a variable called plex4 and simply call javaMethod('createExplicitSimplexStream', [plex4, 'api.Plex4']) in your code instead, etc, which makes it slightly less cumbersome.

    Have fun.

    0 讨论(0)
提交回复
热议问题