Edit this page on our live server and create a PR by running command !create-pr in the console panel

Working with Matlab and Octave

  • Difficulty level: easy
  • Time need to lean: 10 minutes or less

MATLAB (Octave)

SoS tries to exchange variables with MATLAB/Octave in the most natural way. For example it converts numpy arrays to MATLAB matrix, and dictionaries to MATLAB struct. It converts Python DataFrame (or R data.frame) to table in MATLAB but to dataframe in Octave because the latter is closer to Python DataFrame.

The convertion of datatype from SoS to MATLAB (Octave) is as followings:

Python condition MATLAB/Octave
None NaN
boolean logical
integer integer
float float
complex complex
str str
char char
Sequence (list, tuple, ...) homogenous type, all numeric vector
Sequence (list, tuple, ...) homogenous type, all char char
Sequence (list, tuple, ...) multiple types cell
set cell
dict struct
numpy.ndarray cell
numpy.matrix matrix
pandas.DataFrame table (MATLAB)
dataframe (Octave)

Python objects in other datatypes are transferred as string "Unsupported datatype".

The convertion of datatype from MATLAB (Octave) to SoS is as followings:

MATLAB length (n) Python
NaN None
logical 1 boolean
integer 1 integer
numeric 1 double
character 1 string
complex 1 complex
logical n > 1 list
integer n > 1 list
complex n > 1 list
numeric n > 1 list
character n > 1 list
struct n > 0 numpy.ndarray
matrix n > 0 numpy.array
cell n > 0 numpy.ndarray
table n > 0 pandas.DataFrame

For example, a R dataframe is transfered as table to MATLAB.

InĀ [1]:
mtcars = dataframe with 32 rows and 11 columns                                  
Src: /var/folders/ys/gnzk0qbx5wbdgm531v82xxljv5yqy8/T/df2oct.csv                
_1    mpg    cyl    disp     hp   drat     wt   qsec     vs     am   gear   carb
Nr double double  double double double double double double double double double
 1 21.000      6 160.000    110 3.9000 2.6200 16.460      0      1      4      4
 2 21.000      6 160.000    110 3.9000 2.8750 17.020      0      1      4      4
 3 22.800      4 108.000     93 3.8500 2.3200 18.610      1      1      4      1
 4 21.400      6 258.000    110 3.0800 3.2150 19.440      1      0      3      1
 5 18.700      8 360.000    175 3.1500 3.4400 17.020      0      0      3      2
 6 18.100      6 225.000    105 2.7600 3.4600 20.220      1      0      3      1
 7 14.300      8 360.000    245 3.2100 3.5700 15.840      0      0      3      4
 8 24.400      4 146.700     62 3.6900 3.1900 20.000      1      0      4      2
 9 22.800      4 140.800     95 3.9200 3.1500 22.900      1      0      4      2
10 19.200      6 167.600    123 3.9200 3.4400 18.300      1      0      4      4
11 17.800      6 167.600    123 3.9200 3.4400 18.900      1      0      4      4
12 16.400      8 275.800    180 3.0700 4.0700 17.400      0      0      3      3
13 17.300      8 275.800    180 3.0700 3.7300 17.600      0      0      3      3
14 15.200      8 275.800    180 3.0700 3.7800 18.000      0      0      3      3
15 10.400      8 472.000    205 2.9300 5.2500 17.980      0      0      3      4
16 10.400      8 460.000    215 3.0000 5.4240 17.820      0      0      3      4
17 14.700      8 440.000    230 3.2300 5.3450 17.420      0      0      3      4
18 32.400      4  78.700     66 4.0800 2.2000 19.470      1      1      4      1
19 30.400      4  75.700     52 4.9300 1.6150 18.520      1      1      4      2
20 33.900      4  71.100     65 4.2200 1.8350 19.900      1      1      4      1
21 21.500      4 120.100     97 3.7000 2.4650 20.010      1      0      3      1
22 15.500      8 318.000    150 2.7600 3.5200 16.870      0      0      3      2
23 15.200      8 304.000    150 3.1500 3.4350 17.300      0      0      3      2
24 13.300      8 350.000    245 3.7300 3.8400 15.410      0      0      3      4
25 19.200      8 400.000    175 3.0800 3.8450 17.050      0      0      3      2
26 27.300      4  79.000     66 4.0800 1.9350 18.900      1      1      4      1
27 26.000      4 120.300     91 4.4300 2.1400 16.700      0      1      5      2
28 30.400      4  95.100    113 3.7700 1.5130 16.900      1      1      5      2
29 15.800      8 351.000    264 4.2200 3.1700 14.500      0      1      5      4
30 19.700      6 145.000    175 3.6200 2.7700 15.500      0      1      5      6
31 15.000      8 301.000    335 3.5400 3.5700 14.600      0      1      5      8
32 21.400      4 121.000    109 4.1100 2.7800 18.600      1      1      4      2