The scilabmagic
extension provides the ability to interact with Scilab. It is provided by the scilab2py
package,
which may be installed using pip
or easy_install
.
To enable the extension, load it as follows:
%load_ext scilab2py.ipython
Loading the extension enables three magic functions: %scilab
, %scilab_push
, and %scilab_pull
.
The first is for executing one or more lines of Scilab, while the latter allow moving variables between the Scilab and Python workspace. Here you see an example of how to execute a single line of Scilab, and how to transfer the generated value back to Python:
x = %scilab [1 2; 3 4];
x
a = [1, 2, 3]
%scilab_push a
%scilab a = a * 2;
%scilab_pull a
a
When using the cell magic, %%scilab
(note the double %
), multiple lines of Scilab can be executed together. Unlike
with the single cell magic, no value is returned, so we use the -i
and -o
flags to specify input and output variables.
%%scilab -i x -o y
y = x + 3;
y
Plot output is automatically captured and displayed, and using the -f
flag you may choose its format (currently, png
, svg
jpg
, and jpeg
are supported).
%%scilab -f svg
xset("fpf"," ")
contour2d(1:10,1:10,rand(10,10),5,rect=[0,0,11,11]);
Example 3D plots.
%%scilab -f jpg
# simple plot using z = f(x, y)
t = [0: 0.3: 2 * %pi]';
z = sin(t) * cos(t');
plot3d(t, t, z);
%%scilab -f png
[x, y] = meshgrid(0:0.1:3);
r = sin(x - 0.5).^2 + cos(y - 0.5).^2;
surf(x, y, r);
Plots can be drawn inline (default) or bring up the Scilab plotting GUI by using the -g (or --gui) flag:
%%scilab -g
plot([1,2,3])
# brings up a Scilab plotting GUI