W3cubDocs

/Octave

11.12 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

© 1996–2018 John W. Eaton
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://octave.org/doc/interpreter/Commands.html