Define Data Classes

This example shows how to subclass Simulink® data classes.

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

Use an example to define data classes

  1. View the +SimulinkDemos data class package in the folder 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.

Manually 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 does 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 open 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. Open 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. Save this file in the +mypkg/@myCustomAttribsfolder, where +mypkg is the folder containing the @Parameter and @Signal folders.

  2. Define a subclass of Simulink.CustomStorageClassAttributes using MATLAB class syntax. For example, consider a custom storage class that defines data using the original identifier but also provides an alternate name for the data in generated code.

    classdef myCustomAttribs < Simulink.CustomStorageClassAttributes
    	properties(PropertyType = 'char')
    		AlternateName = '';
    	end
    end % classdef
    
  3. Override the default implementation of the isAddressable method to determine whether the custom storage class is writable.

    classdef myCustomAttribs < Simulink.CustomStorageClassAttributes
    		properties(PropertyType = 'logical scalar')
    			IsAlternateNameInstanceSpecific = true;
    		end
    		
    		methods
    			function retVal = isAddressable(hObj, hCSCDefn, hData)
    				retVal = false;
    			end
    		end % methods
    end % classdef
    
  4. Override the default implementation of the getInstanceSpecificProps method.

    For an example, see CSCTypeAttributes_FlatStructure.m in the folder matlabroot\toolbox\simulink\simulink\dataclasses\+Simulink\@CSCTypeAttributes_FlatStructure.

      Note:   This is an optional step. By default, all custom attributes are instance-specific and are modifiable for each data object. However, you can limit which properties are allowed to be instance-specific.

  5. Override the default implementation of the getIdentifiersForInstance method to define identifiers for objects of the data class.

      Note:   In its default implementation, this method queries the name or identifier of the data object and uses that identifier in generated code. By overriding this method, you can control the identifier of your data objects in generated code.

    classdef myCustomAttribs < Simulink.CustomStorageClassAttributes
    	properties(PropertyType = 'char')
    		GetFunction = '';
    		SetFunction = '';
    	end
     
    	methods
    		function retVal = getIdentifiersForInstance(hCSCAttrib, hCSCDefn, hData, identifier)
    			retVal = struct('GetFunction', hData.CoderInfo.CustomAttributes.GetFunction, ...
    			'SetFunction', hData.CoderInfo.CustomAttributes.SetFunction);
    		end%
    	end % methods
    end % classdef
  6. If you are using grouped custom storage classes, override the default implementation of the getIdentifiersForGroup method to specify the identifier for the group in generated code.

    For an example, see CSCTypeAttributes_FlatStructure.m in the folder matlabroot\toolbox\simulink\simulink\dataclasses\+Simulink\@CSCTypeAttributes_FlatStructure.

Was this topic helpful?