blink1073 / oct2py

Run M Files from Python - GNU Octave to Python bridge
http://blink1073.github.io/oct2py/
MIT License
261 stars 54 forks source link

Problem using M files #186

Open jliou369 opened 3 years ago

jliou369 commented 3 years ago

Here is the oct2py document suggested ways of using m files with oct2py

from oct2py import octave octave.addpath('/path/to/') octave.myscript(1, 2)

or

octave.eval("myscript(1, 2)")

as feval

octave.feval('/path/to/myscript', 1, 2)

so I wrote a simple script to try it out, and among ways, only "octave.eval worked, but without the arguments, here is the code, note: I use Oct2Py so that I can use the logger at DEBUG level

from oct2py import octave, Oct2Py, get_log import logging config: OS: Windows 10, oct2py: 5.2.0, Python: 3.8.2, octave: 6.1.0 ======= oct.py =========== oc = Oct2Py(logger=get_log()) oc.logger = get_log('new_log') oc.logger.setLevel(logging.DEBUG)

oc.addpath('C:/Users/jliou/wta21') oc.hello_world()

oc.eval("hello_world")

octave.feval('C:/Users/jliou/wta21/hello_world.m')

===============================

===== hello_world.m ================= disp("Hello World from m file I"); disp("Hello World from m file II"); disp("Hello World from m file III");

------- Output of oc.helloworld() NOT WORKING----------

C:\Users\jliou\wta21>python oct.py Octave eval: exist("addpath") ans = 5

Octave eval: set(0, 'defaultfigurevisible', 'on'); graphics_toolkit('fltk'); Octave eval: _pyeval("C:/Users/jliou/AppData/Local/Temp/tmpsy5z5l57/writer.mat", "C:/Users/jliou/AppData/Local/Temp/tmpsy5z5l57/reader.mat"); Octave eval: exist("hello_world") ans = 2

Octave eval: set(0, 'defaultfigurevisible', 'on'); graphics_toolkit('fltk'); Octave eval: _pyeval("C:/Users/jliou/AppData/Local/Temp/tmpsy5z5l57/writer.mat", "C:/Users/jliou/AppData/Local/Temp/tmpsy5z5l57/reader.mat"); {'message': "octave_base_value::int_value (): wrong type argument 'matrix'", 'identifier': '', 'stack': {'file': 'C:\Python38\Lib\site-packages\oct2py\_pyeval.m', 'name': '_pyeval', 'line': 72.0, 'column': 11.0}} Traceback (most recent call last): File "oct.py", line 10, in oc.hello_world() File "C:\Python38\lib\site-packages\oct2py\dynamic.py", line 96, in call return self._ref().feval(self.name, *inputs, **kwargs) File "C:\Python38\lib\site-packages\oct2py\core.py", line 380, in feval return self._feval(func_name, func_args, dname=dname, nout=nout, File "C:\Python38\lib\site-packages\oct2py\core.py", line 582, in _feval raise Oct2PyError(msg) oct2py.utils.Oct2PyError: Octave evaluation error: error: octave_base_value::int_value (): wrong type argument 'matrix'

----------- Output of oc.eval("hello_world") WORKING---------------------- C:\Users\jliou\wta21>python oct.py Octave eval: exist("addpath") ans = 5

Octave eval: set(0, 'defaultfigurevisible', 'on'); graphics_toolkit('fltk'); Octave eval: _pyeval("C:/Users/jliou/AppData/Local/Temp/tmphb2bhkq8/writer.mat", "C:/Users/jliou/AppData/Local/Temp/tmphb2bhkq8/reader.mat"); Octave eval: graphics_toolkit('gnuplot') set(0, 'defaultfigurevisible', 'off'); Octave eval: _pyeval("C:/Users/jliou/AppData/Local/Temp/tmphb2bhkq8/writer.mat", "C:/Users/jliou/AppData/Local/Temp/tmphb2bhkq8/reader.mat"); Hello World from m file I Hello World from m file II Hello World from m file III

------- Output of oc.addpath('C:/Users/jliou/wta21') NOT WORKING ----------------- C:\Users\jliou\wta21>python oct.py Octave eval: exist("addpath") ans = 5

Octave eval: set(0, 'defaultfigurevisible', 'on'); graphics_toolkit('fltk'); Octave eval: _pyeval("C:/Users/jliou/AppData/Local/Temp/tmp1avijbt7/writer.mat", "C:/Users/jliou/AppData/Local/Temp/tmp1avijbt7/reader.mat"); Traceback (most recent call last): File "oct.py", line 12, in octave.feval('C:/Users/jliou/wta21/hello_world.m') File "C:\Python38\lib\site-packages\oct2py\core.py", line 380, in feval return self._feval(func_name, func_args, dname=dname, nout=nout, File "C:\Python38\lib\site-packages\oct2py\core.py", line 582, in _feval raise Oct2PyError(msg) oct2py.utils.Oct2PyError: Octave evaluation error: error: octave_base_value::int_value (): wrong type argument 'matrix'

BTW, for arguments like oc.eval("hello_world(1,2)", none of the three options worked. Let me know if you want the debug output.

Thanks for the help!

blink1073 commented 2 years ago

Hi @jliou369, the default nout is 1, which causes an error when the script does not return anything. If am able to run oc.hello_world(nout=0) using your example. We used to try and infer nout from the call, but it turned out to be too brittle with the CPython interpreter.