Next: , Previous: , Up: Functions and Scripts   [Contents][Index]

11.13 Commands

Commands are a special class of functions that only accept string input arguments. A command can be called as an ordinary function, but it can also be called without the parentheses. For example,

my_command hello world

is equivalent to

my_command ("hello", "world")

The general form of a command call is

cmdname arg1 arg2

which translates directly to

cmdname ("arg1", "arg2", …)

Any regular function can be used as a command if it accepts string input arguments. For example:

toupper lower_case_arg
   ⇒ ans = LOWER_CASE_ARG

One difficulty of commands occurs when one of the string input arguments is stored in a variable. Because Octave can’t tell the difference between a variable name and an ordinary string, it is not possible to pass a variable as input to a command. In such a situation a command must be called as a function. For example:

strvar = "hello world";
toupper strvar
   ⇒ ans = STRVAR
toupper (strvar)
   ⇒ ans = HELLO WORLD