[Top][All Lists]

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

Re: Command line parameters

From: Daryl Lee
Subject: Re: Command line parameters
Date: Tue, 26 Apr 2011 15:08:07 -0600
User-agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv: Gecko/20110303 Lightning/1.0b2 Thunderbird/3.1.9

On 4/26/2011 1:34 PM, John W. Eaton wrote:
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 ();


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

   arg_list = { ... };

to first define arg_list then execute the script.


That works nicely. Thanks! I'm scrambling around trying to get a real bit of work done and figure out a whole new tool to do it with. All the help is greatly appreciated.

Daryl Lee

reply via email to

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