Octave:解析命令输出

时间:2017-04-24 13:49:50

标签: octave

通常,我希望能够解析Octave命令的输出,例如pkg list image。不幸的是,我无法将输出分配给字符串,例如s = pkg list image

我知道能够做到这一点并不是绝对必要的,因为doint pkg load thispackagedoesnotexist时的错误消息已经足够清楚,但我仍然想知道它是否是&#39 ; s是可能的,如果是的话,如何。

3 个答案:

答案 0 :(得分:2)

您不需要解析Octave函数的打印输出。 Octave函数返回值,而检查这些值则更简单明了。

在您的特定情况下,您只想检查是否已安装软件包。因此,只需检查包是否出现在pkg:

installed = cellfun (@(x) x.name, pkg ("list"),
                     "UniformOutput", false);
if (! any (strcmp (installed, "foo")))
    # Package foo is not installed
endif

或检查其描述是否为空:

if (isempty (pkg ("describe", "foo"){1}))
    # Package foo is not installed
endif

答案 1 :(得分:0)

使用系统命令启动另一个八度音阶:

[output,text]=system("echo 'pkg list image' |octave -q 2>/dev/null",true);

输出是:

output = 0
text = Package Name    | Version | Installation directory
----------------+---------+-----------------------
data-smoothing *|   1.3.0 | /usr/share/octave/packages/data-smoothing-1.3.0
     dataframe  |   0.9.1 | /usr/share/octave/packages/dataframe-0.9.1
       general *|   1.3.4 | /usr/share/octave/packages/general-1.3.4
            io *|   2.0.2 | /usr/share/octave/packages/io-2.0.2
linear-algebra *|   2.2.0 | /usr/share/octave/packages/linear-algebra-2.2.0
 miscellaneous *|   1.2.0 | /usr/share/octave/packages/miscellaneous-1.2.0
          nnet *|  0.1.13 | /usr/share/octave/packages/nnet-0.1.13
        odepkg *|   0.8.4 | /usr/share/octave/packages/odepkg-0.8.4
         optim *|   1.3.0 | /usr/share/octave/packages/optim-1.3.0
   optiminterp *|   0.3.4 | /usr/share/octave/packages/optiminterp-0.3.4
      parallel *|   2.2.0 | /usr/share/octave/packages/parallel-2.2.0
          plot *|   1.1.0 | /usr/share/octave/packages/plot-1.1.0
       splines *|   1.2.6 | /usr/share/octave/packages/splines-1.2.6
    statistics *|   1.2.3 | /usr/share/octave/packages/statistics-1.2.3
       strings *|   1.1.0 | /usr/share/octave/packages/strings-1.1.0
        struct *|  1.0.10 | /usr/share/octave/packages/struct-1.0.10
      symbolic *|   1.1.0 | /usr/share/octave/packages/symbolic-1.1.0

此外,您可以列出所有包裹。从八度文档:

'list'
          Show the list of currently installed packages.  For example,

               installed_packages = pkg ("list")

          returns a cell array containing a structure for each installed
          package.

          If two output arguments are requested 'pkg' splits the list of
          installed packages into those which were installed by the
          current user, and those which were installed by the system
          administrator.

               [user_packages, system_packages] = pkg ("list")

          The option "-forge" lists packages available at the
          Octave-Forge repository.  This requires an internet connection
          and the cURL library.  For example:

               oct_forge_pkgs = pkg ("list", "-forge")

答案 2 :(得分:0)

很难说出你真正想要对输出做什么,但是如果你以正确的方式调用它,pkg函数完全有可能将信息返回到一个有用的变量中。我建议你查看pkg函数的帮助,看一下它将返回的函数形式和类型 https://www.gnu.org/software/octave/doc/interpreter/Installing-and-Removing-Packages.html

例如:

[desc, flag] = pkg ("describe", "secs1d", "image")

将返回flag,它将告诉您包的已安装或加载状态。