Matlab for Python Users Cheat Sheet
Matlab for Python Users Cheat Sheet
The MATLAB language is designed primarily for math-intensive scientific computing. MATLAB combines a desk-
top environment tuned for iterative analysis with a programming language that expresses matrix and array
mathematics directly. Understanding the philosophy and API design can help while learning MATLAB.
» Functions
Creating functions You can declare functions within a function z = foo(x,y)
function file. Input arguments are ...
captured in parentheses. end
Multiple outputs are captured with function [a,b] = foo(x,y)
square brackets. ...
end
Calling functions with input arguments y = foo(x,y,"Name",Value)
and name-value pairs
mathworks.com
» Data Types » Control Flow
Similar data types:
Statement Example
for for i = 1:10
Python MATLAB ...
float double, single end
if if x<3
complex complex single, complex double
...
int (u)int8, (u)int16, (u)int32, (u)int64 elseif x == 2
else
float(nan) NaN ...
end
float(inf) inf
while while x<3
str str, char ...
end
bool logical
switch-case switch switch _ arg
dict struct ...
case case _ arg
list, tuple cell ...
end
pandas.dataframe table try-catch try
...
MATLAB defaults to store all numeric values as double-precision floating-point catch
numbers. Python stores some numbers as integers and others as floating-point ...
numbers. In MATLAB, for x=4 and y=4.0, x is always equal to y. end
» Objects
Define a class Use a class
classdef MyClass • Save the class definition with the same name as the class
properties MyClass.m
MyProp • Create an object of the class
end a = MyClass
methods • Access the properties
function obj = MyClass(val) a.MyProp
end • Call methods to perform operations
function y = MyMethod(obj,x) b = MyMethod(a,val)
end • To pass-by-reference, create a “handle” class
end classdef myclass < handle
end ...
end
mathworks.com
© 2021 The MathWorks, Inc. MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See mathworks.com/trademarks for a list of additional trademarks. 9/21
Other product or brand names may be trademarks or registered trademarks of their respective holders.