I\'m trying to execute some Matlab
scripts (not a function definition) from Python 3
using oct2py
module.
Those scripts (a large
Assuming your script places the results on the (virtual) octave workspace, you can simply attempt to access the workspace.
Example:
%% In file myscript.m
a = 1
b = 2
Python code:
>>> octave.run('myscript.m')
>>> vars = octave.who(); vars
[u'A__', u'a', u'b']
>>> octave.a()
1.0
>>> octave.b()
2.0
Some notes / caveats:
run
command. Your octave current directory may not be the same as your python current directory (this depends on how the octave engine is started). For me python started in my home directory but octave started in my desktop directory. I had to manually check and go to the correct directory, i.e.:
octave.pwd()
octave.cd('/path/to/my/homedir')
Those weird looking variables A__
(B__
, etc) in the workspace reflect the most recent arguments you passed into functions via the oct2py engine (but for some reason they can't be called like normal variables). E.g.
>>> octave.plus(1,2)
3.0
>>> print octave.who()
[u'A__', u'B__', u'a', u'b']
>>> octave.eval('A__')
A__ = 1
>>> octave.eval('B__')
B__ = 2
As you may have noticed from above, the usual ans
variable is not kept in the workspace. Do not rely on any script actions that reference ans
. In the context of oct2py
it seems that ans
will always evaluate to None