Raketenolli
Raketenolli

Reputation: 745

Octave: Parse output of command

Generally, I would like to be able to parse output of Octave commands such as pkg list image. Unfortunately, I cannot assign the output to a string, such as s = pkg list image.

I know that it's not absolutely necessary to be able to do this, because the error message when doint pkg load thispackagedoesnotexist is clear enough, but I'd still like to know whether it's possible, and if yes, how.

Upvotes: 1

Views: 173

Answers (3)

Nick J
Nick J

Reputation: 1610

It's difficult to tell what you actually want to do with the output, but it is entirely possible that the pkg function will return information into a useful variable for you if you call it the right way. I recommend you check out the help for pkg function and look at the functional form and the type of things it will return https://www.gnu.org/software/octave/doc/interpreter/Installing-and-Removing-Packages.html

For example:

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

will return flag which will tell you the installed or loaded state of the package.

Upvotes: 0

carandraug
carandraug

Reputation: 13091

You should not need to parse the printed output of Octave functions. Octave functions return values, and it's much simpler and obvious to check those values instead.

In your specific case, you only want to check if a package is installed. So just check if the package appears listed in pkg:

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

or check if its description is empty:

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

Upvotes: 3

Michael
Michael

Reputation: 5335

Use system command launching another copy of octave:

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

output is:

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

Also, you can list all packages. From octave documentation:

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

Upvotes: 1

Related Questions