help-octave
[Top][All Lists]
Advanced

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

Re: Passing values


From: Mike Miller
Subject: Re: Passing values
Date: Sun, 12 May 2002 14:48:59 -0500 (CDT)

On Sun, 12 May 2002, Mike Miller wrote:

> In an octave script, can I use command-line arguments as in a shell
> script.  Simple example (which does not work):
>
> #!/usr/local/bin/octave -q
> x = $1 ;
> x^2


Ahh, now I know...

#!/usr/local/bin/octave -q
x = str2num(argv(1,:));
disp(x^2)

that does it.  The idea is to use the built-in variable 'argv'.

Octave reads in arguments as strings, so the strings sometimes have to be
converted to numbers.  I'm finding this a little tricky -- when the input
consists of strings of variable length, it looks like the shorter strings
get padded with spaces at the ends.  The strings then have to be truncated
to delete the terminal spaces -- anybody have a script (or simple command)
that does that?

Simple example of where it would be useful:

----------begin script named 'squared'-------
#!/usr/local/bin/octave -q
format(argv(1,:))
x = str2num(argv(2,:));
disp(x^2)
----------end script named 'squared'-------

Commands from the unix prompt would look like this:

squared long .123
squared short .789

The first argument tells octave which output format to use.  The problem
is that this works only if the second argument is not longer (in number of
characters) than the first argument!  If the second argument is longer,
the first argument will have appended spaces, and octave doesn't know what
to do with things like format('long  ').  So what I need is a command
called 'despace' (or whatever) that will fix things:

format(despace('long  '))   or   format(despace(argv(1,:)))

despace would take a string as input and it would return the same string
as output, but with any spaces from the end of the string removed.

Thanks in advance for your tips!

Mike



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

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