14.9.1 Tutorial

sage: matlab('4+10')                     # optional
14
sage: matlab('date')                    # optional; random output
18-Oct-2006
sage: matlab('5*10 + 6')                 # optional
56
sage: matlab('(6+6)/3')                  # optional
4
sage: matlab('9')^2                      # optional
81
sage: a = matlab(10); b = matlab(20); c = matlab(30)    # optional
sage: avg = (a+b+c)/3                    # optional
sage: avg                                # optional
20
sage: parent(avg)                        # optional
Matlab

sage: my_scalar = matlab('3.1415')       # optional
sage: my_scalar                          # optional
3.1415
sage: my_vector1 = matlab('[1,5,7]')     # optional
sage: my_vector1                         # optional
1     5     7
sage: my_vector2 = matlab('[1;5;7]')     # optional
sage: my_vector2                         # optional
1
5
7
sage: my_vector1 * my_vector2            # optional
75

sage: row_vector1 = matlab('[1 2 3]')             # optional
sage: row_vector2 = matlab('[3 2 1]')             # optional
sage: matrix_from_row_vec = matlab('[%s; %s]'%(row_vector1.name(), row_vector2.name()))     # optional
sage: matrix_from_row_vec                            # optional
1     2     3
3     2     1

sage: column_vector1 = matlab('[1;3]')               # optional
sage: column_vector2 = matlab('[2;8]')               # optional
sage: matrix_from_col_vec = matlab('[%s %s]'%(column_vector1.name(), column_vector2.name()))                                    # optional
sage: matrix_from_col_vec                            # optional
1     2
3     8

sage: my_matrix = matlab('[8, 12, 19; 7, 3, 2; 12, 4, 23; 8, 1, 1]')    # optional
sage: my_matrix                                      # optional
     8    12    19
     7     3     2
    12     4    23
     8     1     1

sage: combined_matrix = matlab('[%s, %s]'%(my_matrix.name(), my_matrix.name()))                                        # optional
sage: combined_matrix                               # optional
 8    12    19     8    12    19
 7     3     2     7     3     2
12     4    23    12     4    23
 8     1     1     8     1     1

sage: tm = matlab('0.5:2:10')                       # optional
sage: tm                                            # optional
0.5000    2.5000    4.5000    6.5000    8.5000

sage: my_vector1 = matlab('[1,5,7]')                # optional
sage: my_vector1(1)                                 # optional
1
sage: my_vector1(2)                                 # optional
5
sage: my_vector1(3)                                 # optional
7

Matrix indexing works as follows:

sage: my_matrix = matlab('[8, 12, 19; 7, 3, 2; 12, 4, 23; 8, 1, 1]')     # optional
sage: my_matrix(3,2)                                # optional
4

Setting using paranthesis cannot work (because of how the Python language works). Use square brackets or the set function:

sage: my_matrix = matlab('[8, 12, 19; 7, 3, 2; 12, 4, 23; 8, 1, 1]')    # optional
sage: my_matrix.set(2,3, 1999)                          # optional
sage: my_matrix                                         # optional
           8          12          19
           7           3        1999
          12           4          23
           8           1           1

Module-level Functions

matlab_console( )

This requires that the optional matlab program be installed and in your PATH, but no optional Sage packages need be installed.

sage: matlab_console()                               # optional and not tested
                               < M A T L A B >
                   Copyright 1984-2006 The MathWorks, Inc.
...
>> 2+3

ans =

5

» quit

Typing quit exits the matlab console and returns you to SAGE. matlab, like SAGE, remembers its history from one session to another.

matlab_version( )

Return the version of Matlab installed.

sage: matlab_version()    # optional matlab package
'7.2.0.283 (R2006a)'

reduce_load_Matlab( )

Class: Matlab

class Matlab
Interface to the Matlab interpreter.

sage: a = matlab('[ 1, 1, 2; 3, 5, 8; 13, 21, 33 ]')    # optional
sage: b = matlab('[ 1; 3; 13]')                         # optional
sage: c = a * b                                         # optional
sage: print c                                           # optional
    30
   122
   505
Matlab( self, [maxread=100], [script_subdirectory=], [logfile=None], [server=None], [server_tmpdir=None])

Functions: console,$ \,$ get,$ \,$ sage2matlab_matrix_string,$ \,$ set,$ \,$ version,$ \,$ whos

get( self, var)

Get the value of the variable var.

sage2matlab_matrix_string( self, A)

Return an matlab matrix from a SAGE matrix.

Input: A SAGE matrix with entries in the rationals or reals.

Output: A string that evaluates to an Matlab matrix.

sage: M33 = MatrixSpace(QQ,3,3)                      
sage: A = M33([1,2,3,4,5,6,7,8,0])
sage: matlab.sage2matlab_matrix_string(A)   # requires optional matlab
'[1, 2, 3; 4, 5, 6; 7, 8, 0]'

Author: David Joyner and William Stein

set( self, var, value)

Set the variable var to the given value.

Special Functions: __init__,$ \,$ __reduce__,$ \,$ _install_hints,$ \,$ _object_class,$ \,$ _quit_string,$ \,$ _read_in_file_command,$ \,$ _start

Class: MatlabElement

class MatlabElement

Functions: set

Special Functions: __getitem__,$ \,$ _matrix_

_matrix_( self, R)

Return Sage matrix from this matlab element.

sage: A = matlab('[1,2;3,4]')       # optional matlab package
sage: matrix(ZZ, A)                 # optional
[1 2]
[3 4]
sage: A = matlab('[1,2;3,4.5]')     # optional matlab package
sage: matrix(RR, A)                 # optional
[1.00000000000000 2.00000000000000]
[3.00000000000000 4.50000000000000]

See About this document... for information on suggesting changes.