使用事件侦听器和 MATLAB UI 查看 Simulink 信号

💥💥💞💞欢迎来到本博客❤️❤️💥💥

🏆博主优势:🌞🌞🌞博客内容尽量做到思维缜密,逻辑清晰,为了方便读者。

⛳️座右铭:行百里者,半于九十。

📋📋📋本文目录如下:🎁🎁🎁

目录

💥1 概述

📚2 运行结果

🎉3 参考文献

🌈4 Matlab代码、Simulink仿真实现


💥1 概述

此应用程序展示了如何通过使用事件侦听器和MATLAB用户界面(UI)来查看Simulink信号。我们使用了add_exec_event_listener函数将侦听器附加到Simulink模型的模块上,并通过这些侦听器在MATLAB用户界面上显示模块的输入和输出。

与传统的自定义S功能块方法相比,使用侦听器来查看信号具有两个优点。首先,模型不需要添加任何特殊的查看块,这样可以避免对模型的"破坏",特别是当模型需要与RTW(Real-Time Workshop)一起使用时。其次,相同的用户界面可以用来查看来自不同模型的信号。

具体而言,该应用程序使用一个名为"simpleModel.mdl"的简单模型,其中包含三个块:正弦波、增益和示波器。用户界面允许启动和停止模型,并且可以调整增益值。示波器块接收的信号值将显示在用户界面上的轴上。请注意,用户界面可以在不打开模型的情况下使用,我们建议关闭模型。

用户界面允许以仿真模式(需要Simulink许可证)或作为通用实时(GRT)可执行文件来运行模型(需要RTW许可证)。对于后一种情况,可以使用RTW的外部模式和TCP/IP协议将数据从运行的可执行文件传输到模型,然后再传输到用户界面。

这个应用程序旨在作为一个演示程序,展示使用MATLAB、Simulink和RTW的各个方面。具体而言,它展示了如何使用命令行功能来创建MATLAB用户界面,启动/停止Simulink模型,将侦听器添加到Simulink块中以便从MATLAB用户界面查看信号,以及如何使用命令行功能构建通用实时(GRT)可执行文件。此外,它还展示了与“实时”运行的代码进行交互的方法(在这个UI中,GRT代码在主机上运行,因此不是严格的实时运行,但它使用外部模式与代码进行通信,因此演示了如果代码真正运行在实时操作系统上,通信将如何进行)。

📚2 运行结果

部分代码:

% 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 axes 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 axes 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 参考文献

文章中一些内容引自网络,会注明出处或引用为参考文献,难免有未尽之处,如有不妥,请随时联系删除。

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

[2]张尤赛,马国军,黄炜嘉,等."信号与系统"Matlab实验仿真教学系统设计[J].现代电子技术, 2010, 33(18):3.DOI:CNKI:SUN:XDDJ.0.2010-18-017.

🌈4 Matlab代码、Simulink仿真实现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

荔枝科研社

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值