View Simulink signals using event listeners and MATLAB UI

Welcome to this blog

Advantages of bloggers:Blog content should be as thoughtful and logical as possible for the convenience of readers.

Motto:He who travels a hundred miles is half as good as ninety.

The directory of this article is as follows:

Table of Contents

1 Overview

2 Operation results

3 References

4 Matlab code, Simulink simulation implementation


1 Overview

This application shows how to view Simulink signals by using event listeners and the MATLAB user interface (UI). We used the add_exec_event_listener function to attach listeners to the modules of the Simulink model and display the input and output of the module on the MATLAB user interface through these listeners.

Using a listener to view signals has two advantages over the traditional custom S function block approach. First of all, the model does not need to add any special viewing blocks, which can avoid “breaking” the model, especially when the model needs to be used with RTW (Real-Time Workshop). Second, the same user interface can be used to view signals from different models.

Specifically, the application uses a simple model called “simpleModel.mdl”, which contains three blocks: sine wave, gain, and oscilloscope. The user interface allows starting and stopping the model, and gain values can be adjusted. The signal values received by the oscilloscope block will be displayed on the axis on the user interface. Please note that the user interface can be used without opening the model, we recommend closing the model.

The user interface allows running the model in simulation mode (requires Simulink license) or as a general real-time (GRT) executable (requires RTW license). For the latter case, RTW’s external mode and the TCP/IP protocol can be used to transfer data from the running executable to the model and then to the user interface.

This application is intended as a demonstration program showing various aspects of using MATLAB, Simulink and RTW. Specifically, it shows how to use command line functions to create a MATLAB user interface, start/stop a Simulink model, add listeners to Simulink blocks to view signals from the MATLAB user interface, and how to use command line functions to build generic real-time (GRT) executable file. Additionally, it demonstrates ways to interact with code that is running “live” (in this UI, the GRT code is running on the host machine, so it’s not strictly live, but it uses external mode to communicate with the code, so it demonstrates How would the communication work if the code was actually running on a real-time operating system).

2 Operation results

Part of the code:

% Do some simple error checking on varargout
error(nargoutchk(0,1,nargout));

% Create the UI if one does not already exist.
% Bring the UI to the front if one does already exist.
hf = findall(0,'Tag',mfilename);
if isempty(hf)
    %Create a UI
    hf = localCreateUI(modelName);
else
    % Bring it to the front
    figure(hf);
end

% populate the output if required
if nargout > 0
    varargout{1} = hf;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%
% Function to create the user interface
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%
function hf = localCreateUI(modelName)

try
    % Create the figure, setting appropriate properties
    hf = figure('Tag',mfilename,...
        'Toolbar','none',...
        'MenuBar','none',...
        'IntegerHandle','off',...
        'Units','normalized',...
        'Resize','off',...
        'NumberTitle','off',...
        'HandleVisibility','callback',...
        'Name',sprintf('Custom UI for controlling %s.mdl',modelName),...
        'CloseRequestFcn',@localCloseRequestFcn,...
        'Visible','off');
    
    % Create an ax on the figure
    ha = axes('Parent',hf,...
        'HandleVisibility','callback',...
        'Unit','normalized',...
        'OuterPosition',[0.25 0.1 0.75 0.8],...
        'Xlim',[0 10],...
        'YLim',[-1 1],...
        'Tag','plotAxes');
    xlabel(ha,'Time');
    ylabel(ha,'Signal Value');
    title(ha,'Signal Value v''s Time');
    grid(ha,'on');
    box(ha,'on');
    
    % Create an edit box containing the model name
    hnl = uicontrol('Parent',hf,...
        'Style','text',...
        'Units','normalized',...
        'Position',[0.05 0.9 0.15 0.03],...
        'BackgroundColor',get(hf,'Color'),...
        'String','Model Name',...
        'HandleVisibility','callback',...
        'Tag','modelNameLabel'); %#ok
    hnl = uicontrol('Parent',hf,...
        'Style','edit',...
        'Units','normalized',...
        'Position',[0.02 0.82 0.21 0.06],...
        'String',sprintf('%s.mdl',modelName),...

% Do some simple error checking on varargout
error(nargoutchk(0,1,nargout));

% Create the UI if one does not already exist.
% Bring the UI to the front if one does already exist.
hf = findall(0,’Tag’,mfilename);
if isempty(hf)
%Create a UI
hf = localCreateUI(modelName);
else
% Bring it to the front
figure(hf);
end

% populate the output if required
if nargout > 0
varargout{1} = hf;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%
% Function to create the user interface
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%
function hf = localCreateUI(modelName)

try
% Create the figure, setting appropriate properties
hf = figure(‘Tag’,mfilename,…
‘Toolbar’,’none’,…
‘MenuBar’,’none’,…
‘IntegerHandle’,’off’,…
‘Units’,’normalized’,…
‘Resize’,’off’,…
‘NumberTitle’,’off’,…
‘HandleVisibility’,’callback’,…
‘Name’,sprintf(‘Custom UI for controlling %s.mdl’,modelName),…
‘CloseRequestFcn’,@localCloseRequestFcn,…
‘Visible’,’off’);

% Create an ax on the figure
ha = axes(‘Parent’,hf,…
‘HandleVisibility’,’callback’,…
‘Unit’,’normalized’,…
‘OuterPosition’,[0.25 0.1 0.75 0.8],…
‘Xlim’,[0 10],…
‘YLim’,[-1 1],…
‘Tag’,’plotAxes’);
xlabel(ha,’Time’);
ylabel(ha,’Signal Value’);
title(ha,’Signal Value v”s Time’);
grid(ha,’on’);
box(ha,’on’);

% Create an edit box containing the model name
hnl = uicontrol(‘Parent’,hf,…
‘Style’,’text’,…
‘Units’,’normalized’,…
‘Position’,[0.05 0.9 0.15 0.03],…
‘BackgroundColor’,get(hf,’Color’),…
‘String’,’Model Name’,…
‘HandleVisibility’,’callback’,…
‘Tag’,’modelNameLabel’); %#ok
hnl = uicontrol(‘Parent’,hf,…
‘Style’,’edit’,…
‘Units’,’normalized’,…
‘Position’,[0.02 0.82 0.21 0.06],…
‘String’,sprintf(‘%s.mdl’,modelName),…

3 References

Some of the content in the article is quoted from the Internet. The source will be indicated or cited as a reference. It is inevitable that there will be some unfinished information. If there is anything inappropriate, please feel free to contact us to delete it.

[1] S. L I D E .Matlab/Simulink UI[J].[2023-10-07].

[2] Zhang Yousai, Ma Guojun, Huang Weijia, et al. “Signal and System” Matlab experimental simulation teaching system design [J]. Modern Electronic Technology, 2010, 33(18):3.DOI:CNKI:SUN:XDDJ .0.2010-18-017.