UP | HOME

Support via Liberapay

Octave Code Blocks in Babel

Babel support for Octave

Introduction

GNU Octave describes itself as a scientific programming language featuring a powerful mathematics-oriented syntax with built-in 2D/3D plotting and visualization tools. The Octave syntax is drop-in compatible with many MATLABĀ® scripts.

Requirements and Setup

GNU Octave can be installed on many operating systems.

Emacs Octave Support has been distributed with Emacs since version 19.35.

To configure Octave code blocks in Org mode, add the appropriate dotted pair to org-babel-load-languages

(org-babel-do-load-languages
 'org-babel-load-languages
 '((octave . t)))

Babel Features for Octave Code Blocks

Header Arguments

There are no Octave-specific header arguments or default header argument values.

Sessions

Babel supports Octave sessions.

Result Types

Results of Octave code evaluation can be captured in scripting mode, as :results output, or functional mode, as :results value.

Differences from Other Languages

The special variable, ans, collects values to return in functional mode.

Examples of Use

Hello World!

In the Org mode buffer:

#+begin_src octave :results output
disp('Hello World!')
#+end_src

HTML export of the Octave code block:

disp('Hello World!')

HTML export of the Octave code block result:

Hello World!

Return Part of a Table in Functional Mode

This example is taken from an Octave Cookbook.

Note the use of the variable, ans, on the final line. Babel support for Octave treats ans as a special variable, returning it in functional mode.

In the Org mode buffer:

#+begin_src octave
samples = struct ("patient", {"Bob", "Kevin", "Bob" , "Andrew"},
                  "age",     { 45  ,  52    ,  45   ,  23     },
                  "protein", {"H2B", "CDK2" , "CDK2", "Tip60" },
                  "tube"   , { 3   ,  5     ,  2    ,  18     }
                  );
ans = reshape ([samples(:).age], size (samples))
#+end_src

HTML export of the Octave code block:

samples = struct ("patient", {"Bob", "Kevin", "Bob" , "Andrew"},
                  "age",     { 45  ,  52    ,  45   ,  23     },
                  "protein", {"H2B", "CDK2" , "CDK2", "Tip60" },
                  "tube"   , { 3   ,  5     ,  2    ,  18     }
                  );
ans = reshape ([samples(:).age], size (samples))

HTML export of the Octave code block result:

45 52 45 23

Construct a Table in Octave

In the Org mode buffer:

#+begin_src octave :results value
ans = [8 6 4; 2 0 -2]
#+end_src

HTML export of the Octave code block:

ans = [8 6 4; 2 0 -2]

HTML export of the Octave code block result:

8 6 4
2 0 -2

Graphical output

In order to get graphical output without leaving open graph windows during evaluation, the following can be used:

#+begin_src octave :results file
figure( 1, "visible", "off" );
sombrero;
print -dpng ../images/octave-chart.png;
ans = "../images/octave-chart.png";
#+end_src

The Octave figure() command opens an invisible graph window where the next plot commands should act. Then, the Octave print command exports the graph to a file.

The header argument, :results file, instructs Babel to interpret the code block result as a file path.

HTML export of the Octave code block:

figure( 1, "visible", "off" );
sombrero;
print -dpng ../images/octave-chart.png;
ans = "../images/octave-chart.png";

HTML export of the Octave code block result:

octave-chart.png

Documentation from the orgmode.org/worg/ website (either in its HTML format or in its Org format) is licensed under the GNU Free Documentation License version 1.3 or later. The code examples and css stylesheets are licensed under the GNU General Public License v3 or later.