classdef()
Object-Oriented Programming
classdef() is a MATLAB®-compatible object-oriented programming function for define a class with properties and instance methods. instances are created with classname(args) and methods are called as obj.methodname(args). the first method parameter receives the instance. Run it online in your browser with SimLab — free, no install, no license required.
Define a class with properties and instance methods. Instances are created with ClassName(args) and methods are called as obj.methodName(args). The first method parameter receives the instance.
Syntax
classdef ClassName
properties
propName = defaultValue
end
methods
function obj = ClassName(args)
% constructor body
end
function out = methodName(obj, args)
% method body
end
end
end
classdef ChildClass < ParentClass % single inheritance
methods (Static)
function r = name(args)
...
end
end % Static methodproperties (Constant) NAME = value end % Class-level constant
Examples — run classdef() in your browser
classdef Point
properties
x = 0
y = 0
end
methods
function obj = Point(xv, yv)
obj.x = xv; obj.y = yv;
end
function d = norm(obj)
d = sqrt(obj.x^2 + obj.y^2);
end
end
end
p = Point(3, 4);
d = p.norm();▶ Runclassdef Vec
properties
v = [0;0]
end
methods
function obj = Vec(arr)
obj.v = arr;
end
function r = plus(a, b)
r = Vec(a.v + b.v);
end
end
end
c = Vec([1;2]) + Vec([3;4]); % [4;6] via overloaded +▶ RunLimitations
Single inheritance only. Operator overloading via plus, minus, mtimes, mrdivide, mpower, eq, ne, lt, gt, uminus. Static methods (methods (Static)) and Constant properties (properties (Constant)) accessible as ClassName.fn() / ClassName.NAME without instantiation. Not supported: events, enumeration, Abstract, Sealed, handle classes, multiple inheritance, Access=private enforcement, Constants referencing other class Constants, Static/Constant inheritance through superclass.
See Also
Frequently Asked Questions about classdef()
Is the classdef() function free to use in SimLab?
The browser workspace requires no MATLAB® license or installation. Connected features require network access and may have account, tier, availability, or usage limits. You can open classdef() and 510+ other MATLAB®-compatible function references at simulations4all.com/simlab.
Do I need to install MATLAB to use classdef()?
No. SimLab’s core parser, workspace state, and most functions run locally in your browser. The classdef() function requires no MATLAB® license or desktop software download.
What does classdef() do?
Define a class with properties and instance methods. Instances are created with ClassName(args) and methods are called as obj.methodName(args). The first method parameter receives the instance. It is a MATLAB®-compatible object-oriented programming function available in SimLab with the same calling syntax as MATLAB® — typically: classdef ClassName.
Can I run classdef() in my browser?
Yes. Open SimLab at simulations4all.com/simlab, write or paste code that calls classdef(), and run it. This function’s calculation runs locally in the browser. SimLab session analytics, sharing, feedback, and eligible acceleration have separate network boundaries.
Try SimLab — MATLAB®-compatible, free, in your browser
510+ functions. Runs in your browser. No install. No license.
Open SimLabMATLAB® is a registered trademark of The MathWorks, Inc. SimLab is an independent project by Simulations4All and is not affiliated with, endorsed by, or sponsored by The MathWorks, Inc.