Limit Property Values to Finite List

String sets are composed of two linked properties:

  • The user-visible property name with a default text value.

  • The associated hidden property with "Set" appended to the user-visible property name with all possible values.

In the hidden Set property, specify the valid text values as a cell array of the matlab.system.StringSet class. This example defines a color string set using Color and ColorSet as the associated properties.

properties
   Color = 'blue'
end

properties (Hidden,Constant)
   ColorSet = matlab.system.StringSet({'red','blue','green'});
end

 Complete Class Definition File with StringSet

Create a Whiteboard System object

This example shows how to use the whiteboard System object. Each time you run the object, it draws a line on a whiteboard.

Display System object used in this example

type('Whiteboard.m');
classdef Whiteboard < matlab.System
    % Whiteboard Draw lines on a figure window
    %
    properties
        Color = 'blue'
    end
    
    properties (Hidden)
        % Choose a color
        ColorSet = matlab.system.StringSet({'red','blue','green'});
    end
    
    methods (Access = protected)
        function stepImpl(obj)
            h = Whiteboard.getWhiteboard();
            plot(h, ...
                randn([2,1]), randn([2,1]), ...
                'Color',obj.Color(1));
        end
        
        function releaseImpl(obj)
            cla(Whiteboard.getWhiteboard());
            hold on
        end
    end
    
    methods (Static)
        function a = getWhiteboard()
            h = findobj('tag','whiteboard');
            if isempty(h)
                h = figure('tag','whiteboard');
                hold on
            end
            a = gca;
        end
    end
end

Construct the System object

hGreenInk = Whiteboard;
hBlueInk  = Whiteboard;

Change the color

hGreenInk.Color = 'green';
hBlueInk.Color = 'blue';

Draw a few lines

for i=1:3
  hGreenInk();
  hBlueInk();
end

Clear the whiteboard

hBlueInk.release();

See Also

Was this topic helpful?