get data from octave script execution using oct2py (python3)

后端 未结 1 443
情歌与酒
情歌与酒 2021-01-23 00:29

I\'m trying to execute some Matlab scripts (not a function definition) from Python 3 using oct2py module.

Those scripts (a large

相关标签:
1条回答
  • 2021-01-23 01:09

    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:

    • I ran into problems when I tried running a script, as it complained I was trying to run it as a function; you can bypass this using the 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

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