Documentation Center

  • Trials
  • Product Updates

Define Data Classes

This example shows how to subclass Simulink® data classes.

Use MATLAB® class syntax to create a data class in a package, and, optionally, assign properties to the data class and define custom storage classes.

Define data class

  1. Create a package folder +mypkg and add its parent folder to the MATLAB path.

  2. Create class folders @Parameter and @Signal inside +mypkg.

      Note:   Simulink requires data classes to be defined inside +Package/@Class folders.

  3. In the @Parameter folder, create a MATLAB file Parameter.m and open it for editing.

  4. Define a data class that is a subclass of Simulink.Parameter using MATLAB class syntax.

    classdef Parameter < Simulink.Parameter
       
    end % classdef
    

Optional: Add properties to data class

The properties and end keywords enclose a property definition block.

See Supported Property Types.

classdef Parameter < Simulink.Parameter
	properties % Unconstrained property type
		Prop1 = [];
	end

	properties(PropertyType = 'logical scalar')
		Prop2 = false;
	end

	properties(PropertyType = 'char')
		Prop3 = '';
	end

	properties(PropertyType = 'char',...
      AllowedValues = {'red'; 'green'; 'blue'})
		Prop4 = 'red';
	end
end % classdef

    Note:   The MATLAB editor will not recognize the attributes PropertyType and AllowedValues, because they are only defined for Simulink data classes. You can suppress mlint warnings about these unrecognized attributes by right-clicking the attribute in the MATLAB editor and selecting Suppress "Unknown attribute name ‘...'" > In This File.

Optional: Add initialization code to data class

You can add a constructor within your data class to perform initialization activities when the class is instantiated.

In this example, the constructor initializes the value of object obj based on an optional input argument.

classdef Parameter < Simulink.Parameter
	methods
		function obj = Parameter(optionalValue)
			if (nargin == 1)
				obj.Value = optionalValue;
			end
		end
	end % methods
end % classdef

Optional: Define custom storage classes

Use the setupCoderInfo method to configure the CoderInfo object of your class. Then, create a call to the useLocalCustomStorageClasses method and launch the Custom Storage Class Designer.

  1. In the constructor within your data class, call the useLocalCustomStorageClasses method.

    classdef Parameter < Simulink.Parameter
    	methods
    		function setupCoderInfo(obj)
    			useLocalCustomStorageClasses(obj, 'mypkg');
    			
    			obj.CoderInfo.StorageClass = 'Custom';
    		end
    	end % methods
    end % classdef
    
  2. Launch the Custom Storage Class Designer for your package.

    cscdesigner('mypkg')
  3. Define custom storage classes. Use Custom Storage Class Designer.

Optional: Define custom attributes for custom storage classes

  1. Create a MATLAB file myCustomAttribs.m and open it for editing.

  2. Define a subclass of Simulink.CustomStorageClassAttributes MATLAB class syntax.

    classdef CSCAttributes < Simulink.CustomStorageClassAttributes
    	properties(PropertyType = 'char')
    		AlternateName = '';
    	end
    end % classdef
    
  3. Override the default implementation of the getInstanceSpecificProps method.

      Note:   This is an optional step. By default, you can set all custom attributes to be instance-specific in the Custom Storage Class designer so that they are modifiable for each data object. However, you can limit which properties are allowed to be instance-specific.

classdef CSCAttributes < Simulink.CustomStorageClassAttributes
	properties(PropertyType = 'char')
		AlternateName = '';
	end % properties

	properties(PropertyType = 'logical scalar')
		IsAlternateNameInstanceSpecific = true;
	end
 
	methods
		function props = getInstanceSpecificProps(obj)
			if obj.IsAlternateNameInstanceSpecific
				props = findprop(obj, 'AlternateName');
			end
		end
	end % methods
end % classdef

Alternate way of defining data classes

  1. Locate the +SimulinkDemos data class package on the MATLAB path.

      Note:   The +SimulinkDemos folder is at matlabroot/toolbox/simulink/simdemos/dataclasses

    This package contains predefined data classes.

  2. Copy the folder to the location where you want to define your data classes.

  3. Rename the folder +mypkg and add its parent folder to the MATLAB path.

  4. Modify the data class definitions as desired.

Was this topic helpful?