MATLAB: Is it possible to create a customised Display block showing only a desired selection of values from a multidimensional input

displaylistenermasksimulink

In my Simulink model I have a multidimensional signal. I would like to visualise only a selection of its values through a customised Display block as the simulation runs.
Ideally, I would like to grab this block from a customised library and be able to choose what values to display for several multidimensional signals in my model.

Best Answer

It is possible to do so by creating a masked subsystem and displaying a desired selection of values from a multidimensional signal on its mask.
Step1
Create a function that allows to select what values to display from a given multidimensional input, and show them on the mask of the subsystem:
function selectorListener(block, ~, name,r1,c1,r2,c2)
u = block.InputPort(1).Data;
str = '';
str = [str '\n 1st value: ' num2str(u(r1,c1)) ];
str = [str '\n 2nd value: ' num2str(u(r2,c2)) ];
str = ['disp(''' str ''' )'];
set_param(name,'MaskDisplay',str);
end
If you wish to display all the values instead:
function displayListener(block, ~, name)
u = block.InputPort(1).Data;
[m,~] = size(u);
str = '';
for i = 1:m
str = [str '\n' num2str(u(i,:)) ];
end
str = ['disp(''' str ''' )'];
set_param(name,'MaskDisplay',str);
end
Step 2
Create a new library:
Step 3
In the library, create a masked subsystem and treat it as atomic unit:
Step 4
Right-click on the subsystem, select Properties, and navigate to the Callbacks tag:
Specify the following callback functions:
- CopyFcn
set_param(gcb, 'LinkStatus', 'none')
This allows to modify the block once it is dragged from the library:
- StartFcn
if ~exist('list_counter')
list_counter = 1;
end
list{list_counter} = add_exec_event_listener(gcb,'PostOutputs',{@selectorListener,gcb,1,1,2,1});
list_counter = list_counter+1;
This allows to create a listener for each block and select what values to show on its mask during simulation:
In the case above, values in position (1,1) and (2,1) are displayed.
If instead you wish to display all the values, consider using:
list{list_counter} = add_exec_event_listener(gcb,'PostOutputs',{@displayListener,gcb});
- StopFcn
clear list list_counter;
This removes the variables needed to create listeners once simulation stops.
Step 5:
Drag and drop the blocks from the library to your Simulink model. You can access the block callbacks and change the values to be displayed in the StartFcn callback.
For further details, please refer to the attachment 'customDisplay.zip' (R2018a).