simulink中模块库的建立和维护
转自:http://blog.sina.com.cn/s/blog_91cc0e2c0101hoto.html
一.模块库的建立与修改
模块库的建立与模型model的建立类似,都是在File菜单栏中,只不过要选择Library而非Model,如下图:
然后再该Library中添加了两个模块,并存为名为Interpolation.mdl文件。如下图:
注意:当模块库被保存后,模块库就自动被锁定,模块库中的模块都无法修改,所以修改时需要Edit->Unlock Library来解锁方能修改。
二.模块库中建子模块库
此处的目的是在HiNOC模块库中建立Interpolation子模块库(上一步已经建好了主体)。
- 首先建立一个模块库保存为HiNOC.mdl;
- 从simulink->Port&Subsystems中复制subsystem模块进HiNOC.mdl中,并改名为Interpolation,将模块内的内容(ports等元件)全部删除;
- 鼠标右键Interpolation模块,从弹出菜单中选中Block properties...选择弹出的对话框中callbacks分页,选择左边栏中的OpenFcn项,在右边编辑已经建立的要包含的子库的文件名(本例中即Interpolation),此时再点击Interpolation的subsystem模块,就会发现此模块里面不再是“空”的了,这涉及到回调(callback)函数,以后再详细讨论。
此时Interpolation的subsystem模块中“不空”了
三.将自定义的模块库加入到simulink模块库列表中
此步骤中欲将刚才建立的HiNOC.mdl模块库加入到simulink模块库列表中,就像系统的模块一样使用。
- 建立一文件夹(本例中命名为HiNOC),并将刚才建立的HiNOC.mdl,Interpolation.mdl等移动到此文件夹中,并将该文件夹加入到matlab的path(matlab搜索路径,在File->Set Path中操作,有时还要在File->Preferrences中点击Update toolbox Path Cache)中;
- 在文件夹HiNOC中加入名为slblocks.m的文件(可从matlabroot/toolbox/simulink/blocks/slblocks.m中获得模板或者参考已安装的模块库中的slblocks.m文件的写法),此处的写法如下:
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 (mdl-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 mdl-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-2006 The MathWorks, Inc.
% $Revision: 1.20.2.10 $
%
% Name of the subsystem which will show up in the Simulink Blocksets
% and Toolboxes subsystem.
%
blkStruct.Name = ['Simulink' sprintf('/n') 'HiNOC'];
%
% The function that will be called when the user double-clicks on
% this icon.
%
blkStruct.OpenFcn = 'HiNOC';
%
% 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:2*pi],sin([0:2*pi]));';
% No display for Simulink Extras.
%
blkStruct.MaskInitialization = '';
x = exp(j*[-45:-8:-215, -45]/180*pi);
x1 = x * 20 + 20 + j*35;
x2 = -x*10 + 60 +j*75;
p_str = ['plot(',...
mat2str(real(x1),2), ',', mat2str(imag(x1),2), ',', ...
'[0 15 ', mat2str(real(x1(10:13)),2),' 0],[0 0 ', mat2str(imag(x1(10:13)),2),' 0],', ...
mat2str(real(x2),2), ',', mat2str(imag(x2),2), ',', ...
'[19 40 35 52 40 49 60],[34 55 65 50 70 64 75],', ...
'[74.5 70 65 74],[84.5 80 85 94],',...
'[66 65 70.5 71], [86 99 97 91],',...
'[75 74 79 80 75], [81 94 92 79 81],',...
'[74.5 73], [87 87],',...
'-10, 0, 100, 100);'];
blkStruct.MaskDisplay = p_str;
%
% 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 = 'simulink';
% Browser(1).Name = 'Simulink';
% Browser(1).IsFlat = 0;% Is this library "flat" (i.e. no subsystems)?
Browser(1).Library = 'HiNOC';
Browser(1).Name = 'HiNOC by dfd1r';
Browser(1).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
如此,就可在simulink的模块库列表中查看到自定义的模块库了,如下图:
四.模块的引用的一些问题
用户从模块库中复制模块到自己的模型中是对模块库中原型的引用,这种被复制到模型中的模块被称为引用块,二者之间存在关联,即如果库中的原始块被修改,则模型中的引用块也将被修改。
判断一个模块是否是引用块,可以选择Format->Library Link Display->All,然后观察模块左下角是否有箭头标志(关联模块有箭头标志),如下图就是关联模块:
若要对模型中的引用块进行修改,可以先取消关联关系,再修改,具体如下:
1. 选中引用块,右键菜单Link Options->Disable Link来取消关联;
2. 此时修改引用块,不会影响到原始块;
3. 此时若要恢复关联,同样右键单击模块,选择右键菜单Link Options->Resolve Link,若果模块进行了修改,就会弹出如下提框:
4. 如果Action中选择push,则会更新库中的原始块以与当前模块相同,完成关联;如果Action中选择Restore,则会更新当前模块与原始块一致,完成关联。
5. 如果要取当前块的关联彻底与模块库断开连接,断开后将无法恢复关联,使用右键Link Options->Break Link。
若Simulink不能按照参考模块的连接找到模块库,则此参考模块会以红色的虚线框显示并给出错误信息,解决这个问题有以下两种方法:
1. 删除此模块在重新设置新的模块;
2. 用鼠标左键双击此模块,在弹出的对话框中填入正确的模块路径。
五.参考资料
matlab 2010版的help文档
《Simulink通信仿真教程》