The object-oriented capability in MATLAB is not strong, and its function can be extended by using the Jython engine.
1 edit classpath.txt and add Jython. Jar
Input in MATLAB
Which classpath.txt
Result:
/Usr/local/Matlab/r2013a/toolbox/local/classpath.txt
Edit the file and add
/Home/your_user/jython2.5.3/Jython. Jar
2. Restart MATLAB 3 and write the code for testing.
import javax.script.Invocable; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; mg = ScriptEngineFactory();jy = mg.getEngineByName('jython')
You can see the result:
JY =
[Email protected]
jy.eval('print("Hello")')jy.eval('def add(a, b):\n return a + b')jy.eval('add(5, 5)')jy.put('name', 'Hello')jy.get('name')v = rand(5, 5)jy.put('vals', v)jy.get('vals')
If you want to use other script engines, such as JavaScript, refer to the previous blog article. 4. Call the Jython interpreter to call the Python code file.
import org.python.util.PythonInterpreterintp = PythonInterpreter();intp.exec('a = 5;')intp.get('a');
But in fact, the exec method and the method in 3 are functionally consistent;
Considering the interface consistency of javacripengine, we recommend that you use the method in step 3.
Using Jython extension in MATLAB