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 16:22:56 -0500 (CDT)

On Sun, 12 May 2002, Mike Miller wrote:

> 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.


I figured it out.  I think this will always work, and it is fairly
efficient.  I welcome any better methods...

Suppose x is a string with trailing spaces and we want to remove the
trailing spaces and name the new truncated string y.  Do this:

x="string    ";
y=x(1:max(find(toascii(x)!=32)));

In my simple example using argv, the script could be written like this:

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


Have a great day.

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]