classdef ClassName
classdef ClassName < SuperClass
classdef ClassName < handle
classdef defines a value or handle class in an M-file.
Nelson supports class properties, methods, events, enumerations with constructor arguments, enumerations based on numeric scalar types, constants, inheritance, dynamic handle properties through dynamicprops, separate method files in @ClassName folders, package classes in +package folders, and continued declarations with ....
Method attributes include Abstract, Access, Hidden, Sealed, and Static. Property attributes include Abstract, Access, GetAccess, SetAccess, AbortSet, Constant, Dependent, GetObservable, SetObservable, Transient, NonCopyable, WeakHandle, and validation expressions.
AbortSet skips handle-property set notifications and setter execution when the assigned value is equal to the stored value.
Transient properties are omitted from struct and persisted object data, and reload with their default values when a default constructor is available. NonCopyable handle properties are reset to their default values when copied through the copy mixin.
WeakHandle properties of a handle class hold their handle values without keeping the referenced objects alive: once an object has no other reference it is destroyed and the property reads back a deleted handle of the same class. Use them for back references (child to parent, listener to source) that would otherwise form a reference cycle, since objects in a cycle of strong references are never destroyed. A WeakHandle property must declare a class validation, for example Parent (1,1) Node, and cannot be Constant or Dependent; without a default value it starts as deleted handles of the validated class and size.
Property validation supports fixed dimensions, type names, validator function names, and validator arguments such as mustBeGreaterThan(0).
Handle subclasses can define events. Event attributes include Hidden, ListenAccess, and NotifyAccess. Class attributes include Abstract, ConstructOnLoad, Hidden, InferiorClasses, and Sealed.
Access attributes accept public, private, protected, a single metaclass name such as ?FriendClass, or a class list such as {?FriendA, ?FriendB}.
Inherited abstract methods and abstract properties must be implemented by concrete subclasses, and conflicting inherited member names are reported when a class is parsed.
Abstract properties can be combined with access, constant, dependent, hidden, and validation attributes. An abstract property declaration must not define an initial value.
Classdef methods can access private members of their own class and protected members of superclasses. Public members are returned by methods, properties, and events.
Supported method forms include constructors, instance methods, static methods, simple single-line method definitions, inherited method dispatch, sealed and abstract declarations, external method files in @ClassName folders, accessor methods named get.PropertyName and set.PropertyName, traditional indexing hooks named subsref, subsasgn, and end, simple dot indexing hooks named dotReference and dotAssign, simple parenthesis indexing hooks named parenReference and parenAssign for value and handle classes, simple brace indexing hooks named braceReference and braceAssign, user delete methods for handle classes, copy from nelson.mixin.Copyable, and protected displayScalarObject methods from nelson.mixin.CustomDisplay.
Special persistence methods saveObjectImpl and static loadObjectImpl can convert objects to and from saved structures. They are applied to scalar objects and element by element for non-empty object arrays.
Classdef object arrays preserve their class metadata during indexed assignment. Newly created value-class elements use the class default constructor, and newly created handle-class elements receive distinct default handles. ClassName.empty(...) creates typed empty object arrays for value and handle classes.
Handle classes inheriting from dynamicprops can add instance properties with addprop(obj, name) and remove them with rmprop(obj, name) or delete(descriptor). Dynamic properties appear in isprop, properties, object field access, and struct conversion. The descriptor returned by addprop is a meta.DynamicProperty handle.
Classdef objects work with Nelson save/load, debugger, profiler, and completion support. Save/load hooks are applied element by element for object arrays, including arrays nested in cells or structs. Empty object arrays keep their class and dimensions when saved and loaded. Handle arrays are checked for invalid elements before save hooks are invoked.
Numeric enumeration classes such as classdef Mode < uint32 use each member argument as the stored numeric value, support isa(member, 'uint32'), and expose the base-type conversion such as uint32(member).
metaclass exposes class, property, method, event, and enumeration metadata. Property metadata includes default value expressions, validation expressions, defining class, access modes, and flags for Constant, Dependent, Abstract, Hidden, GetObservable, SetObservable, AbortSet, Transient, NonCopyable, and Dynamic. Method metadata includes defining class, access, and flags for Static, Hidden, Sealed, and Abstract. Event metadata includes ListenAccess, NotifyAccess, Hidden, and raw attributes. Enumeration metadata includes member names, defining class, and constructor arguments.
d = [tempdir(), 'nelson_help_classdef/'];
mkdir(d);
filewrite([d, '/NelsonHelpPoint.m'], ["classdef NelsonHelpPoint"; " properties"; " X = 0"; " Y = 0"; " end"; " methods"; " function obj = NelsonHelpPoint(x, y)"; " if nargin > 0"; " obj.X = x;"; " obj.Y = y;"; " end"; " end"; " end"; "end"]);
addpath(d);
p = NelsonHelpPoint(3, 4);
p.X
d = [tempdir(), 'nelson_help_classdef/'];
mkdir(d);
filewrite([d, '/NelsonHelpSavedValue.m'], ["classdef NelsonHelpSavedValue"; " properties"; " Value = 0"; " end"; " methods"; " function obj = NelsonHelpSavedValue(value)"; " if nargin > 0"; " obj.Value = value;"; " end"; " end"; " function data = saveObjectImpl(obj)"; " data = struct('Value', obj.Value);"; " end"; " end"; " methods (Static)"; " function obj = loadObjectImpl(data)"; " obj = NelsonHelpSavedValue(data.Value);"; " end"; " end"; "end"]);
addpath(d);
obj = NelsonHelpSavedValue(12);
data = obj.saveObjectImpl();
copy = NelsonHelpSavedValue.loadObjectImpl(data);
fileName = [tempdir(), 'nelson_help_classdef_object.nh5'];
save(fileName, 'obj');
clear obj;
load(fileName);
[copy.Value, obj.Value]
| Version | Description |
|---|---|
| 2.0.0 | initial version |