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 method
properties (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();
▶ Run
classdef 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 +
▶ Run

Limitations

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?

Yes. SimLab is 100% free — no signup, no license, and no usage limits. You can run classdef() and 510+ other MATLAB®-compatible functions directly in your browser at simulations4all.com/simlab.

Do I need to install MATLAB to use classdef()?

No. SimLab runs entirely in your web browser with zero installation. The classdef() function works without any MATLAB® license or 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. Execution happens locally in your browser — no data leaves your machine.

Try SimLab — MATLAB®-compatible, free, in your browser

510+ functions. Runs in your browser. No install. No license.

Open SimLab

MATLAB® 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.

Stay Updated

Get notified about new simulations and tools. We send 1-2 emails per month.