使用oct2py(python3)从八度脚本执行获取数据 [英] get data from octave script execution using oct2py (python3)

查看:413
本文介绍了使用oct2py(python3)从八度脚本执行获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用oct2py模块从Python 3执行一些Matlab脚本(不是函数定义).

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

这些脚本(大量)包含一个扩展的定义,以读取特定的ASCIII文件(包含在同一目录中).

Those scripts (a large amount) contains a very extended definition to read a specific ASCIII files (contained in the same directory).

我不知道如何使用Matlab(八度)脚本通过Python读取数据.

I do not know how to get the data read by Python with the Matlab (octave) scripts.

这是我在做什么:

from oct2py import octave

import numpy as np
import os

import pprint

hom_dir='/path_to/files&scripts_dir/'
os.chdir(hom_dir)

octave.addpath(/path_to/files&scripts_dir/')

out=octave. matlab_file # (matlab_file.m)

输出:

Out[237]: <function oct2py.core.Oct2Py._make_octave_command.<locals>.octave_command>"

pprint.pprint(out)

<function Oct2Py._make_octave_command.<locals>.octave_command at 0x7f2069d669d8>"

没有返回错误,但是我不知道如何获取数据(在Octave会话中读取的数据).我发现的示例使用oct2py执行.m文件,其中有关定义函数的文件,但这不是我的情况.

No error is returned, but I do not know how to get the data (that were read in a Octave session). The examples that I have found for execute .m files using oct2py where about files that define functions, however that is not my case.

推荐答案

假设您的脚本将结果放置在(虚拟)倍频程工作区中,则您只需尝试访问该工作区即可.

Assuming your script places the results on the (virtual) octave workspace, you can simply attempt to access the workspace.

示例:

%% In file myscript.m
a = 1
b = 2

Python代码:

>>> octave.run('myscript.m')
>>> vars = octave.who(); vars
[u'A__', u'a', u'b']
>>> octave.a()
1.0
>>> octave.b()
2.0

一些注释/警告:

  • 尝试运行脚本时遇到问题,因为它抱怨我试图将其作为函数运行;您可以使用run命令绕过此操作.
  • 您的八度当前目录可能与python当前目录不同(这取决于八度引擎的启动方式).对我来说,python在我的主目录中启动,但是八度在我的桌面目录中启动.我必须手动检查并转到正确的目录,即:

  • 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')

  • 工作空间中那些看起来很奇怪的变量A__(B__等)反映了您通过oct2py引擎传递给函数的最新参数(但是由于某些原因,它们不能像普通变量一样被调用).例如

  • 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
    

  • 从上面可能已经注意到,通常的ans变量未保留在工作空间中.不要依赖引用ans的任何脚本操作.在oct2py的上下文中,看来ans总是会评估为None

  • 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

    这篇关于使用oct2py(python3)从八度脚本执行获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆