simulink中创建自定义模块库

(这几天想要把几个常用模块添入Simulink Library Browser库,作为库模块调用,在网上查了很多相关帖子或者博文,结合matlab帮助文档,如愿搭好自己希望的库模块,记录下,有需要的可以看看)
1.simulink中创建自定义模块库需满足:
(1)建立Library的mdl或slx文件,在文件中加入自己的模块;
(2)建立slblocks脚本,用于定义模块库在Simulink Library Browser中的显示;
(3)建立Library的mdl或slx文件与slblocks脚本放在同路径下,并将路径添加到MATLAB的Set Path中。
2.建模实例展示
(1).建立Library的mdl或slx文件(存盘为VCU_Library的mdl文件)
在这里插入图片描述
(2)建立slblocks脚本(下列源代码中斜体部分需要根据Library名及显示名更改,也可以按图片写脚本@远上寒衫)
function blkStruct = slblocks
%SLBLOCKS Defines the block library for a specific Toolbox or Blockset.
% SLBLOCKS returns information about a Blockset to Simulink. The
% information returned is in the form of a BlocksetStruct with the
% following fields:
%
% Name Name of the Blockset in the Simulink block library
% Blocksets & Toolboxes subsystem.
% OpenFcn MATLAB expression (function) to call when you
% double-click on the block in the Blocksets & Toolboxes
% subsystem.
% MaskDisplay Optional field that specifies the Mask Display commands
% to use for the block in the Blocksets & Toolboxes
% subsystem.
% Browser Array of Simulink Library Browser structures, described
% below.
%
% The Simulink Library Browser needs to know which libraries in your
% Blockset it should show, and what names to give them. To provide
% this information, define an array of Browser data structures with one
% array element for each library to display in the Simulink Library
% Browser. Each array element has two fields:
%
% Library File name of the library (model file) to include in the
% Library Browser.
% Name Name displayed for the library in the Library Browser
% window. Note that the Name is not required to be the
% same as the model file name.
%
% Example:
%
% %
% % Define the BlocksetStruct for the Simulink block libraries
% % Only simulink_extras shows up in Blocksets & Toolboxes
% %
% blkStruct.Name = [‘Simulink’ sprintf(’\n’) ‘Extras’];
% blkStruct.OpenFcn = ‘simulink_extras’;
% blkStruct.MaskDisplay = sprintf(‘Simulink\nExtras’);
%
% %
% % Both simulink and simulink_extras show up in the Library Browser.
% %
% blkStruct.Browser(1).Library = ‘simulink’;
% blkStruct.Browser(1).Name = ‘Simulink’;
% blkStruct.Browser(2).Library = ‘simulink_extras’;
% blkStruct.Browser(2).Name = ‘Simulink Extras’;
%

% Copyright 1990-2013 The MathWorks, Inc.

%
% Name of the subsystem which will show up in the Simulink Blocksets
% and Toolboxes subsystem.
%
blkStruct.Name = [‘Simulink’ sprintf(’\n’) ‘Extras’];

%
% The function that will be called when the user double-clicks on
% this icon.
%
blkStruct.OpenFcn = ‘simulink_extras’;

%
% The argument to be set as the Mask Display for the subsystem. You
% may comment this line out if no specific mask is desired.
% Example: blkStruct.MaskDisplay = ‘plot([0:2pi],sin([0:2pi]));’;
% No display for Simulink Extras.
%
blkStruct.MaskDisplay = ‘’;

%
% Define the Browser structure array, the first element contains the
% information for the Simulink block library and the second for the
% Simulink Extras block library.
%
Browser(1).Library = ‘VCU_Library’;
Browser(1).Name = ‘My_VCU’;
Browser(1).IsFlat = 1
;% Is this library “flat” (i.e. no subsystems)?

Browser(2).Library = ‘simulink_extras’;
Browser(2).Name = ‘Simulink Extras’;
Browser(2).IsFlat = 0;% Is this library “flat” (i.e. no subsystems)?

blkStruct.Browser=Browser;
clear Browser;
% Define information about Signal Viewers
%
Viewer(1).Library = ‘simviewers’;
Viewer(1).Name = ‘Simulink’;

blkStruct.Viewer = Viewer;
clear Viewer;

%
% Define information about Signal Generators
%
Generator(1).Library = ‘simgens’;
Generator(1).Name = ‘Simulink’;

blkStruct.Generator = Generator;
clear Generator;

% Define information for model updater
blkStruct.ModelUpdaterMethods.fhDetermineBrokenLinks = @UpdateSimulinkBrokenLinksMappingHelper;
blkStruct.ModelUpdaterMethods.fhSeparatedChecks = @UpdateSimulinkBlocksHelper;

% End of slblocks
在这里插入图片描述
注:Browser中的IsFlat 用于设计显示是否含有下一层目录(0,含目录;1,不含目录),默认状态下为0
(3)设置路径
在这里插入图片描述
完成后重启或F5刷新Simulink Library Browser库
3.完成后向自定义库中新增模块或更改编辑
打开建立Library的mdl或slx文件(如VCU_Library),,选择菜单栏中的diagrdm—>Unlock Library解锁后操作
在这里插入图片描述
注:完成后要刷新Simulink Library Browser库才可以显示编辑
4.删除自定义库
将MATLAB的Set Path中自定义库的路径删除;

  • 2
    点赞
  • 41
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论

打赏作者

weixin_42711131

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

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值