|On this page…|
The class block is the code that starts with the classdef key word and terminates with the end key word. The following example illustrated a simple class definition that uses:
Property set and get functions
Use of a delete method for the handle object
Static method syntax
You can display this class definition in a separate window that contains links to related sections in the documentations by clicking this link:
The following code defines a class called topo. It is derived from handle so it is a handle class, which means it references the data it contains. See Using the topo Class for information on how this class behaves.
classdef topo < handle % topo is a subclass of handle properties FigHandle % Store figure handle FofXY % function handle Lm = [-2*pi 2*pi]; % Initial limits end % properties properties (Dependent, SetAccess = private) Data end % properties Dependent = true, SetAccess = private methods function obj = topo(fnc,limits) % Constructor assigns property values obj.FofXY = fnc; obj.Lm = limits; end % topo function set.Lm(obj,lim) % Lm property set function if ~(lim(1) < lim(2)) error('Limits must be monotonically increasing') else obj.Lm = lim; end end % set.Lm function data = get.Data(obj) % get function calculates Data % Use class name to call static method [x,y] = topo.grid(obj.Lm); matrix = obj.FofXY(x,y); data.X = x; data.Y = y; data.Matrix = matrix;% Return value of property end % get.Data function surflight(obj) % Graph function as surface obj.FigHandle = figure; surfc(obj.Data.X,obj.Data.Y,obj.Data.Matrix,... 'FaceColor',[.8 .8 0],'EdgeColor',[0 .2 0],... 'FaceLighting','phong'); camlight left; material shiny; grid off colormap copper end % surflight method function delete(obj) % Delete the figure h = obj.FigHandle; if ishandle(h) delete(h); else return end end % delete end % methods methods (Static = true) % Define static method function [x,y] = grid(lim) inc = (lim(2)-lim(1))/35; [x,y] = meshgrid(lim(1):inc:lim(2)); end % grid end % methods Static = true end % topo class
See Commented Example Code for information on using this class.
This class is designed to display a combination surface/contour graph of mathematical functions of two variables evaluated on a rectangular domain of x and y. For example, any of the following functions can be evaluated over the specified domain (note that x and y have the same range of values in this example just for simplicity).
x.*exp(-x.^2 - y.^2); [-2 2] sin(x).*sin(y); [-2*pi 2*pi] sqrt(x.^2 + y.^2); [-2*pi 2*pi]
To create an instance of the class, passing a function handle and a vector of limits to the constructor. The easiest way to create a function handle for these functions is to use an anonymous function:
tobj = topo(@(x,y) x.*exp(-x.^2-y.^2),[-2 2]);
The class surflight method uses the object to create a graph of the function. The actual data required to create the graph is not stored. When the surflight method accesses the Data property, the property's get function performs the evaluation and returns the data in the Data property structure fields. This data is then plotted. The advantage of not storing the data is the reduced size of the object.
The topo class is defined as a handle class. This means that instances of this class are handle objects that reference the underlying data store created by constructing the object. For example, suppose you create an instance of the class and create a copy of the object:
tobj = topo(@(x,y) x.*exp(-x.^2-y.^2),[-2 2]); a = tobj; surflight(a) % Call class method to create a graph
Now suppose you change the FofXY property so that it contains a function handle that points to another function:
tobj.FofXY = @(x,y) y.*exp(-x.^2-y.^2); % now multiply exp by y instead of x surflight(a)
Because a is a copy of the handle object tobj, changes to the data referenced by tobj also change the data referenced by a.
If topo were a value class, the objects tobj and a would not share data; each would have its own copy of the property values.