Main Content

Customize MATLAB System Block Appearance

This example shows how to customize the appearance of the MATLAB System block. The model contains three MATLAB System blocks. The first block does not have any customization for block appearance. By default, the block shows the name of the System object™. The argument names in the System object stepImpl method provide the port labels. The second block shows custom text and custom port labels on the block icon. The third block shows a custom block icon image.

System Objects

System objects allow you to implement algorithms using MATLAB®. System objects are a specialized kind of MATLAB object, designed specifically for implementing and simulating dynamic systems with inputs that change over time.

After you define a System object, you can include it in a Simulink® model using a MATLAB System block.

System Object Class Definition

You can access the MATLAB System block source code by clicking the "Source code" hyperlink in the block dialog. The TimesTwo System object used in the first block has no customization and implements only the stepImpl method. The CustomBlockIconExample System object implements these methods for customizing block appearance.

  • getInputNamesImpl - Customize input port labels

  • getOutputNamesImpl - Customize output port labels

  • getIconImpl - Display text or an image on the block

The System object has a DisplayImage property to choose between text and image for display on the block.

TimesTwo System object

classdef TimesTwo < matlab.System
%TimesTwo Multiply input by 2
%   obj = TimesTwo returns a System object, obj, that 
%   multiples its input by two.

    methods(Access = protected)
        function y = stepImpl(~, u)
            y = 2 * u;
        end
    end
end

CustomBlockIconExample System object

classdef CustomBlockIconExample < matlab.System 
% SystemObjectBlockIconExample Customize Block Icon 

    properties(Nontunable)
        % DisplayImage Select to display image as block icon
        DisplayImage (1,1) logical = false
    end

    methods(Access = protected)
        function y = stepImpl(~, u)
            y = u;
        end
        function inputName = getInputNamesImpl(~)
        	inputName = "MyIn";
        end
        function outputName = getOutputNamesImpl(~)
        	outputName = "MyOut";
        end
        function icon = getIconImpl(obj)
            % Return text or image to be displayed on the block icon
            % Use array of strings to display multiple lines of text
            if obj.DisplayImage
                % Display image
                icon = matlab.system.display.Icon('slexngc6543aPix.jpg');
            else
                % Display text
                icon = ["Block icon", "with custom text"];
            end
        end
    end
end

See Also

| |

Related Topics