getpythonvar

Imports the Python variable value to the OML variable.

Syntax

[value,status,errorMessage]=getpythonvar(PythonVariableName)

Inputs

PythonVariableName
Python variable name.
Type: string

Outputs

status
Status of the script executed.
1
success
0
failure
Type: number
errorMessage
Error message indicating the failure.
Type: string
value
OML variable.
Type:
Python Variable Type OML Variable Type Limitations
Bool Logical  
long, Float Number  
Complex Complex  
List Cell (1,n) n:number of elements in list Does not support if list contains Dict (with limitation), Tupple, Set
dict Struct Supports only if keys in dict are string or char.
Numpy - array, matrix Matrix Data types supported in OML: matrix, Bool, Int, long, Float, Complex.
Numpy Ndarray ND Matrix Data types supported in OML: matrix, Bool, Int, long, Float, Complex.
     
Scipy - CSC (Compressed Sparse Column Matrix) Sparse Matrix Convert Python Scipy non-CSC sparse matrix to CSC using the method tocsc() to import it to OML.
Pandas DataFrame Table Followig are supported: 1. Row names as strings or no Row names 2. Column names as strings 3. Data in given column is homogeneous data type 4. Data in given column is string or number data type 5. Column names should be unique

Examples

Import logical data from Python:
evalpythonscript('pofalse = False'); 
[value, status, errormessage] = getpythonvar('pofalse')
value = 0
status = 1
errormessage =
Import integer data from Python:
evalpythonscript('point = 999'); 
[value, status, errormessage] = getpythonvar('point')
value = 999
status = 1
errormessage =
Import precision data from Python:
evalpythonscript('pofloat=9.99');
[value, status, errormessage] = getpythonvar('pofloat')
value = 9.99
status = 1
errormessage =
Import complex data from Python:
evalpythonscript('pocomp=9+9j');
[value, status, errormessage] = getpythonvar('pocomp')
value = 9 + 9i
status = 1
errormessage =
Import string data from Python:
evalpythonscript('postring="String Data"');
[value, status, errormessage] = getpythonvar('postring')
value = String Data
status = 1
errormessage =
Import list data from Python:
evalpythonscript('polist  = ["a","b","c","d"]');
[value, status, errormessage] = getpythonvar('polist')
value = 
{
[1,1] a
[1,2] b
[1,3] c
[1,4] d
}
status = 1
errormessage =
Import dictionary data from Python:
evalpythonscript('polist  = ["a","b","c","d"]');
evalpythonscript('podict = {"key1":1,"key2" : polist }');
[value, status, errormessage] = getpythonvar('podict')
value = struct [
key1: 1
key2: 
{
[1,1] a
[1,2] b
[1,3] c
[1,4] d
]
status = 1
errormessage =
Import matrix data from Python:
evalpythonscript('import numpy as np');
evalpythonscript('pomatrix = np.matrix("1 2; 3 4")');
[value, status, errormessage] = getpythonvar('pomatrix')
value = [Matrix] 2 x 2
1 2
3 4
status = 1
errormessage =
Import nd matrix data from Python:
evalpythonscript('import numpy as np');
evalpythonscript('pondarray = np.arange(720).reshape(6,4,2,3,5)');
[value, status, errormessage] = getpythonvar('pondarray')
value = 
slice(:, :, 1, 1, 1) = 
[Matrix] 6 x 4
0 30 60 90
120 150 180 210
240 270 300 330
360 390 420 450
480 510 540 570
600 630 660 690
slice(:, :, 1, 1, 2) = 
[Matrix] 6 x 4 Row[1] Columns[1:4]
1 31 61 91
[Matrix] 6 x 4 Rows[2:6] Columns[1:4]
121 151 181 211
241 271 301 331
361 391 421 451
481 511 541 571
601 631 661 691
…….
slice(:, :, 2, 3, 4) = 
[Matrix] 6 x 4
28 58 88 118
148 178 208 238
268 298 328 358
388 418 448 478
508 538 568 598
628 658 688 718
slice(:, :, 2, 3, 5) = 
[Matrix] 6 x 4 Row[1] Columns[1:4]
29 59 89 119
[Matrix] 6 x 4 Rows[2:6] Columns[1:4]
149 179 209 239
269 299 329 359
389 419 449 479
509 539 569 599
629 659 689 719

status = 1
errormessage =
Import Scipy CSC sparse matrix data from Python.
evalpythonscript('import numpy as np;from scipy.sparse import csc_matrix');
evalpythonscript('row = np.array([0, 2, 2, 0, 1, 2])');
evalpythonscript('col = np.array([0, 0, 1, 2, 2, 2])');
evalpythonscript('data = np.array([1, 2, 3, 4, 5, 6])');
evalpythonscript('csc = csc_matrix((data, (row, col)), shape=(3, 3))');
csc=getpythonvar('csc')
csc = sparse [3 x 3], nnz = 6
[1,1] 1
[3,1] 2
[3,2] 3
[1,3] 4
[2,3] 5
[3,3] 6
Import Scipy non-CSC sparse matrix data from Python.
evalpythonscript('import numpy as np;from scipy.sparse import csr_matrix');
evalpythonscript('row = np.array([0, 2, 2, 0, 1, 2])');
evalpythonscript('col = np.array([0, 0, 1, 2, 2, 2])');
evalpythonscript('data = np.array([1, 2, 3, 4, 5, 6])');
evalpythonscript('csr=csr_matrix((data, (row, col)), shape=(3, 3))');
evalpythonscript('csc=csr.tocsc()');
csc=getpythonvar('csc')
csc = sparse [3 x 3], nnz = 6
[1,1] 1
[3,1] 2
[3,2] 3
[1,3] 4
[2,3] 5
[3,3] 6 
Import Pandas dataframe from Python:
evalpythonscript('import pandas as pd; df = pd.DataFrame({"col1": [1, 2], "col2": [3, 4]})');
df=getpythonvar('df');
disp(df)

col1 col2
____ ____
1 3
2 4
	
Import Pandas dataframe from Python:
evalpythonscript('import pandas as pd; df = pd.DataFrame({"col1": [1, 2], "col2": [3, 4]}, index = ["row1", "row2"])');
df=getpythonvar('df');
disp(df)

col1 col2
    ____ ____
row1 1 3
row2 2 4