1

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.

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

3 Comments

pkg("describe", "package_thats_not_installed") will throw an error and not populate any variable I'd like to assign the function return value to, but the function acting on the pkg("list") output works very well (assuming at least some packages are installed; couldn't test for zero packages installed) and is close enough to what I'd like to do.
@Raketenolli if you are using Octave 4.2.0, isempty (pkg ("describe", "foo"){1}) like I have on the answer, it works. If you are getting an error, you must be using Octave 4.0.X or older.
Yes, I'm using Octave 4.0.0
1

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.