Now that the recipe book is available for both MATLAB and Python, many Python users will also look into the MATLAB version and discover the beauty of older sister of Python. There is hope for these people, especially if they want to switch: you can call Python in MATLAB! Here is how.
pyenv('Version','~/opt/anaconda3/bin/python')
After that, we have four Options 1–4 to use Python commands and scripts in MATLAB.
Option 1
d = pyrun("c = a**2","c",a=2)
yields the output
d = 4
in the Command Window of MATLAB.
Option 2
np = py.importlib.import_module('numpy')
np = Python module with properties: fft: [1×1 py.module] eye: [1×1 py.function] format_float_scientific: [1×1 py.function] array_repr: [1×1 py.function] str_: [1×1 py.type] (cont'd)
A = np.array([[2,4,3,7], [9,3,-1,2], [1,9,3,7], [6,6,3,-2]])
A = Python ndarray: Columns 1 through 14 2 4 3 7 9 3 -1 2 1 9 3 7 6 6 Columns 15 through 16 3 -2 Use details function to view the properties of the Python object. Use double function to convert to a MATLAB array.
A = np.array([[2,4,3,7]; [9,3,-1,2]; [1,9,3,7]; [6,6,3,-2]])
A = Python ndarray: 2 4 3 7 9 3 -1 2 1 9 3 7 6 6 3 -2 Use details function to view the properties of the Python object. Use double function to convert to a MATLAB array.
details(A)
py.numpy.ndarray handle with properties: T: [1×1 py.numpy.ndarray] base: [1×1 py.NoneType] ctypes: [1×1 py.numpy.core._internal._ctypes] data: [1×4 py.memoryview] dtype: [1×1 py.numpy.dtype[float64]] flags: [1×1 py.numpy.core.multiarray.flagsobj] flat: [1×1 py.numpy.flatiter] imag: [1×1 py.numpy.ndarray] itemsize: [1×1 py.int] nbytes: [1×1 py.int] ndim: [1×1 py.int] real: [1×1 py.numpy.ndarray] shape: [1×2 py.tuple] size: [1×1 py.int] strides: [1×2 py.tuple] Methods, Events, Superclasses
whos("A")
Name Size Bytes Class Attributes A 1x1 8 py.numpy.ndarray
m1 = mean(A)
m1 = 3.8750
C = double(A) m2 = mean(C)
C = 2 4 3 7 9 3 -1 2 1 9 3 7 6 6 3 -2 m2 = 4.5000 5.5000 2.0000 3.5000
Option 3
import numpy as np B = np.array([[2,4,3,7], [9,3,-1,2], [1,9,3,7], [6,6,3,-2]]) print(B) np.who() import scipy.io as sio sio.savemat('mydata.mat', {'B':B})
pyrunfile("py_example_script.py") load mydata.mat
[[ 2 4 3 7] [ 9 3 -1 2] [ 1 9 3 7] [ 6 6 3 -2]] Name Shape Bytes Type ===================================== B 4 x 4 128 int64 Upper bound on total bytes = 128
We can then load the data from data.mat using
B = load('mydata.mat')
which loads the data into the MATLAB workspace.
Option 4
def examplefunction(): ''' Example Function to demonstrate the use of user-created module''' print('examplefunction executed') import numpy as np B = np.array([[2,4,3,7], [9,3,-1,2], [1,9,3,7], [6,6,3,-2]]) return B
pyrun("from examplefunction import examplefunction") pyrun("help(examplefunction)") D = pyrun("B = examplefunction()","B")
Help on function examplefunction in module examplefunction: examplefunction() Example Function to demonstrate the use of user-created module examplefunction executed D = Python ndarray: 2 4 3 7 9 3 -1 2 1 9 3 7 6 6 3 -2 Use details function to view the properties of the Python object. Use int64 function to convert to a MATLAB array.
References
Trauth, M.H. (2022) Python Recipes for Earth Sciences – First Edition. Springer International Publishing, ~453 p., Supplementary Electronic Material, Hardcover, ISBN 978-3-031-07718-0.