Customize System Block Appearance Programmatically - MATLAB & Simulink (original) (raw)
Main Content
You can customize the icon for a MATLAB System block by adding descriptive text, port labels, and icon image.
Note
From R2023b, you can use the Mask Editor to design a MATLAB System block icon. You can also migrate existing icon customizations to the Mask Editor. This capability eliminates the need to develop or maintain the getInputNamesImpl
, getOutputNamesImpl
, andgetIconImpl
methods in a System object™ file. For more information, see Customize MATLAB System Icon and Dialog Box Using Mask Editor.
Specify Input and Output Names
Specify the names of the input and output ports of a System object–based block implemented using a MATLAB System block.
Use getInputNamesImpl
and getOutputNamesImpl
to specify the names of the input port as “source data” and the output port as “count.”
If you do not specify the getInputNamesImpl
andgetOutputNamesImpl
methods, the object uses thestepImpl
method input and output variable names for the input and output port names, respectively. If the stepImpl
method uses_varargin_ and varargout instead of variable names, the port names default to empty character vectors.
methods (Access = protected) function inputName = getInputNamesImpl(~) inputName = 'source data'; end
function outputName = getOutputNamesImpl(~) outputName = 'count'; end end
Complete Class Definition with Named Inputs and Outputs
classdef MyCounter < matlab.System
% MyCounter Count values above a threshold
properties
Threshold = 1
end
properties (DiscreteState)
Count
end
methods
function obj = MyCounter(varargin)
setProperties (obj,nargin,varargin{:});
end
end
methods (Access = protected)
function setupImpl(obj)
obj.Count = 0;
end
function resetImpl(obj)
obj.Count = 0;
end
function y = stepImpl(obj,u)
if (u > obj.Threshold)
obj.Count = obj.Count + 1;
end
y = obj.Count;
end
function inputName = getInputNamesImpl(~)
inputName = 'source data';
end
function outputName = getOutputNamesImpl(~)
outputName = 'count';
end
end
end
Add Text to Block Icon
Add text to the block icon of a System object–based block implemented using a MATLAB System block.
Subclass from custom icon class.
classdef MyCounter < matlab.System & matlab.system.mixin.CustomIconUse
getIconImpl
to specify the block icon asNew Counter
with a line break between the two words.
methods (Access = protected)
function icon = getIconImpl(~)
icon = {'New','Counter'};
end
endComplete Class Definition File with Defined Icon
classdef MyCounter < matlab.System & ...
matlab.system.mixin.CustomIcon% MyCounter Count values above a threshold
properties
Threshold = 1
end
properties (DiscreteState)
Count
endmethods
function obj = MyCounter(varargin)
setProperties(obj,nargin,varargin{:});
end
endmethods (Access = protected)
function setupImpl(obj)
obj.Count = 0;
end
function resetImpl(obj)
obj.Count = 0;
end
function y = stepImpl(obj,u)
if (u > obj.Threshold)
obj.Count = obj.Count + 1;
end
y = obj.Count;
end
function icon = getIconImpl(~)
icon = {'New','Counter'};
end
end
end
Add Image to Block Icon
Define an image on the block icon of a System object–based block implemented using a MATLAB System block.
Subclass from custom icon class.
classdef MyCounter < matlab.System & matlab.system.mixin.CustomIconUse
getIconImpl
method to call thematlab.system.display.Icon
class and specify the image.
methods (Access = protected)
function icon = getIconImpl(~)
icon = matlab.system.display.Icon('counter.png');
end
endComplete Class Definition File with Icon Image
classdef MyCounter < matlab.System & ...
matlab.system.mixin.CustomIcon% MyCounter Count values above a threshold
properties
Threshold = 1
end
properties (DiscreteState)
Count
endmethods
function obj = MyCounter(varargin)
setProperties(obj,nargin,varargin{:});
end
endmethods (Access = protected)
function setupImpl(obj)
obj.Count = 0;
end
function resetImpl(obj)
obj.Count = 0;
end
function y = stepImpl(obj,u)
if (u > obj.Threshold)
obj.Count = obj.Count + 1;
end
y = obj.Count;
end
function icon = getIconImpl(~)
icon = matlab.system.display.Icon('counter.png');
end
end
end