help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Command line parameters


From: John W. Eaton
Subject: Re: Command line parameters
Date: Tue, 26 Apr 2011 15:34:51 -0400

On 26-Apr-2011, Daryl Lee wrote:

| On 4/26/2011 11:36 AM, Martin Helm wrote:
| > Am Dienstag, 26. April 2011, 19:20:58 schrieb Daryl Lee:
| >> I'm fairly new to Octave.  I've figured out how to pass command-line
| >> arguments to a script, but only if that script is an executable script or
| >> called from the system command line, a la "$ octave myscript.m arg1 arg2".
| >> Is there a way to do so from the Octave console?  When I am at the Octave
| >> prompt and type "myscript arg1 arg2" I get "error: invalid use of script in
| >> index expression."  I'm using Octave 3.2.4 on a Windows 7 64-bit laptop.
| >>
| >> Here is the script that I have so far:
| >> ====================
| >> % Display command line
| >>
| >> printf('%s; %d arguments\n', program_name(), nargin);
| >> arg_list = argv();
| >> for i = 1:nargin
| >>      printf('%d argument = %s\n', i, arg_list{i});
| >> endfor
| >>
| >> printf('\n');
| >> ====================
| >
| > I am really not sure if I understood what you want. But what about
| > system("octave myscript.m a b")
| >
| > at the octave command prompt?
| >
| >
| 
| Well, that works, but the value of arg_list (or any other variable, I'm 
| guessing) isn't available to me after the script terminates.  It seems to me 
| (again, perhaps naively) that one primary reason for using the console in 
| the first place is to be to be able to go in after a script runs and examine 
| the values as a debugging step.

Scripts in Octave do not accept arguments, but functions do, so maybe
you want to use functions instead, and have your function return the
values you need?

But if you insist on using a script, you just need to define the
variables it needs before calling it since the script is executed in
the same top-level scope as the commands you type at the prompt.  So
you could write something like

  if (! exist ("arg_list", "var"))
    arg_list = argv ();
  endif

  ...

in your script so that it would use the argv function if arg_list was
not already defined.  When calling it from the command line, you could
use

  arg_list = { ... };
  myscript

to first define arg_list then execute the script.

jwe


reply via email to

[Prev in Thread] Current Thread [Next in Thread]