Documentation Center

  • Trial Software
  • Product Updates

Limit Property Values to Finite String Set

This example shows how to limit a property to accept only a finite set of string values.

Specify a Set of Valid String Values

String sets use two related properties. You first specify the user-visible property name and default string value. Then, you specify the associated hidden property by appending "Set" to the property name. You must use a capital "S" in "Set."

In the "Set" property, you specify the valid string values as a cell array of the matlab.system.Stringset class. This example uses Color and ColorSet as the associated properties.

properties
   Color = 'blue'
end

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

Complete Class Definition File with String Set

classdef Whiteboard < matlab.System
%Whiteboard Draw lines on a figure window
%
% This System object™ illustrates the use of StringSets

   properties
     Color = 'blue'
   end
    
   properties (Hidden,Transient)
     % Let them 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
     function n = getNumInputsImpl(~)
          n = 0;
     end
     function n = getNumOutputsImpl(~)
         n = 0;
     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

String Set System Object Example

%%
% Each call to step draws lines on a whiteboard

%% Construct the System object  
hGreenInk = Whiteboard;
hBlueInk  = Whiteboard;

% Change the color
% Note: Press tab after typing the first single quote to 
% display all enumerated values.
hGreenInk.Color = 'green';
hBlueInk.Color  = 'blue';

% Take a few steps
for i=1:3
  hGreenInk.step();
  hBlueInk.step();
end

%% Clear the whiteboard
hBlueInk.release();

%% Display System object used in this example
type('Whiteboard.m');

See Also

Was this topic helpful?