MATLAB Object-Oriented Programming

(Joyce) #1
classdef BasicHandle < handle
properties
Prop1
end
methods
function obj = BasicHandle(val)
if nargin > 0
obj.Prop1 = val;
end
end
end
end

Create a BasicHandle object and use it in a switch statement:

h1 = BasicHandle('Handle Object');
h2 = h1;

Here is the switch statement code:

switch h1
case h2
disp('h2 is selected')
otherwise
disp('h2 not selected')
end

The result is:

h2 is selected

Object Must Be Scalar

The switch statements work only with scalar objects. For example:

h1(1) = BasicHandle('Handle Object');
h1(2) = BasicHandle('Handle Object');
h1(3) = BasicHandle('Handle Object');
h2 = h1;

switch h1
case h2
disp('h2 is selected')
otherwise
disp('h2 not selected')
end

5 Class Definition—Syntax Reference

Free download pdf