Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
GNU Octave
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Notable features== ===Command and variable name completion=== Typing a TAB character on the command line causes Octave to attempt to complete variable, function, and file names (similar to [[Bash (Unix shell)|Bash]]'s [[tab completion]]). Octave uses the text before the cursor as the initial portion of the name to complete.<ref>{{cite web|url=https://www.gnu.org/software/octave/doc/interpreter/Commands-For-Completion.html#Commands-For-Completion|title=Letting Readline Type For You|work=GNU Octave Reference Manual|last=Eaton|first=John W.|access-date=2016-07-29|archive-date=2018-02-12|archive-url=https://web.archive.org/web/20180212145842/http://www.gnu.org/software/octave/doc/interpreter/Commands-for-Completion.html#Commands-For-Completion|url-status=dead}}</ref> ===Command history=== When running interactively, Octave saves the commands typed in an internal buffer so that they can be recalled and edited. ===Data structures=== Octave includes a limited amount of support for organizing data in structures. In this example, we see a structure {{mono|x}} with elements {{mono|a}}, {{mono|b}}, and {{mono|c}}, (an integer, an array, and a string, respectively): <syntaxhighlight lang="octave"> octave:1> x.a = 1; x.b = [1, 2; 3, 4]; x.c = "string"; octave:2> x.a ans = 1 octave:3> x.b ans = 1 2 3 4 octave:4> x.c ans = string octave:5> x x = scalar structure containing the fields: a = 1 b = 1 2 3 4 c = string </syntaxhighlight> ===Short-circuit Boolean operators=== Octave's <code>&&</code> and <code>||</code> logical [[Operator (programming)|operators]] are evaluated in a [[Short-circuit evaluation|short-circuit]] fashion (like the corresponding operators in the [[C (programming language)|C]] language), in contrast to the element-by-element operators <code>&</code> and <code>|</code>. ===Increment and decrement operators=== {{Main|Increment and decrement operators}} Octave includes the C-like [[increment and decrement operators]] <code>++</code> and <code>--</code> in both their prefix and postfix forms. Octave also does [[augmented assignment]], e.g. <code>x += 5</code>. ===Unwind-protect=== Octave supports a limited form of [[exception handling]] modelled after the [http://www.lispworks.com/documentation/HyperSpec/Body/s_unwind.htm <code>unwind_protect</code>] of [[Lisp (programming language)|Lisp]]. The general form of an unwind_protect block looks like this: <syntaxhighlight lang="octave"> unwind_protect body unwind_protect_cleanup cleanup end_unwind_protect </syntaxhighlight> As a general rule, GNU Octave recognizes as termination of a given <code>''block''</code> either the keyword <code>end</code> (which is compatible with the MATLAB language) or a more specific keyword <code>end''block''</code> or, in some cases, <code>end_''block''</code>. As a consequence, an <code>unwind_protect</code> block can be terminated either with the keyword <code>end_unwind_protect</code> as in the example, or with the more portable keyword <code>end</code>. The ''cleanup'' part of the block is always executed. In case an exception is raised by the ''body'' part, ''cleanup'' is executed immediately before propagating the exception outside the block <code>unwind_protect</code>. GNU Octave also supports another form of exception handling (compatible with the MATLAB language): <syntaxhighlight lang="matlab"> try body catch exception_handling end </syntaxhighlight> This latter form differs from an <code>unwind_protect</code> block in two ways. First, ''exception_handling'' is only executed when an exception is raised by ''body''. Second, after the execution of ''exception_handling'' the exception is not propagated outside the block (unless a <code>rethrow( lasterror )</code> statement is explicitly inserted within the ''exception_handling'' code). ===Variable-length argument lists=== Octave has a mechanism for handling functions that take an unspecified number of arguments without explicit upper limit. To specify a list of zero or more arguments, use the special argument <code>varargin</code> as the last (or only) argument in the list. <code>varargin</code> is a cell array containing all the input arguments. <syntaxhighlight lang="octave"> function s = plus (varargin) if (nargin==0) s = 0; else s = varargin{1} + plus (varargin{2:nargin}); end end </syntaxhighlight> ===Variable-length return lists=== A function can be set up to return any number of values by using the special return value <code>varargout</code>. For example: <syntaxhighlight lang="octave"> function varargout = multiassign (data) for k=1:nargout varargout{k} = data(:,k); end end </syntaxhighlight> ===C++ integration=== It is also possible to execute Octave code directly in a C++ program. For example, here is a code snippet for calling <code>rand([10,1])</code>: <syntaxhighlight lang="cpp"> #include <octave/oct.h> ... ColumnVector NumRands(2); NumRands(0) = 10; NumRands(1) = 1; octave_value_list f_arg, f_ret; f_arg(0) = octave_value(NumRands); f_ret = feval("rand", f_arg, 1); Matrix unis(f_ret(0).matrix_value()); </syntaxhighlight> C and C++ code can be integrated into GNU Octave by creating oct files, or using the MATLAB compatible [[MEX file]]s.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)