MATLAB Object-Oriented Programming

(Joyce) #1
function hsvObj = HSVColor(obj)
if isa(obj,'RGBColor')
hsvObj = HSVColor(rgb2hsv(obj.Color));
end
end
end
end

Create an array of RGBColor and HSVColor objects with the revised superclass:

crgb = RGBColor([1 0 0]);
chsv = HSVColor([0 1 1]);
ary = [crgb,chsv];
class(ary)

ans =

RGBColor

MATLAB calls the converter method for the HSVColor object, which it inherits from the
superclass. The second array element is now an RGBColor object with an RGB color
specification assigned to the Color property:

ary(2)

ans =

RGBColor with properties:

Color: [1 0 0]

ary(2).Color

ans =

1 0 0

If the leftmost object is of class HSVColor, the array ary is also of class HSVColor, and
MATLAB converts the Color property data to HSV color specification.

ary = [chsv crgb]

ary =

1x2 HSVColor

10 Object Arrays

Free download pdf