simulink中模块库的建立和维护


分类: Matlab |
一.模块库的建立与修改
http://hi.csdn.net/attachment/201103/25/2857981_1301041389kwCK.jpg
http://hi.csdn.net/attachment/201103/25/2857981_1301041397jmC5.jpg
注意:当模块库被保存后,模块库就自动被锁定,模块库中的模块都无法修改,所以修改时需要Edit->Unlock Library来解锁方能修改。
二.模块库中建子模块库
- 首先建立一个模块库保存为HiNOC.mdl;
- 从simulink->Port&Subsystems中复制subsystem模块进HiNOC.mdl中,并改名为Interpolation,将模块内的内容(ports等元件)全部删除;http://hi.csdn.net/attachment/201103/25/2857981_13010414041p0v.jpg
- 鼠标右键Interpolation模块,从弹出菜单中选中Block properties...选择弹出的对话框中callbacks分页,选择左边栏中的OpenFcn项,在右边编辑已经建立的要包含的子库的文件名(本例中即Interpolation),此时再点击Interpolation的subsystem模块,就会发现此模块里面不再是“空”的了,这涉及到回调(callback)函数,以后再详细讨论。
http://hi.csdn.net/attachment/201103/25/2857981_1301041410A2u7.jpg
http://hi.csdn.net/attachment/201103/25/2857981_13010414190Mpz.jpg
此时Interpolation的subsystem模块中“不空”了
http://hi.csdn.net/attachment/201103/25/2857981_13010414283Rgy.jpg
三.将自定义的模块库加入到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 inthe block the Blocksets & Toolboxes -
%
subsystem. -
%
MaskDisplay Optional field that specifies the Mask Display commands -
%
to use for the inblock 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 inlibrary to display 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 inlibrary 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 forMask Display the subsystem. You -
%
may comment this line outif no isspecific mask 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 forSimulink block library and the second 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
= @UpdateSimulinkBrokenLink sMappingHelper; -
%
blkStruct.ModelUpdaterMethods.fhSeparatedChecks = -
%
@UpdateSimulinkBlocksHelp er; -
%
End of slblocks
http://hi.csdn.net/attachment/201103/25/2857981_130104144133y8.jpg
四.模块的引用的一些问题
http://hi.csdn.net/attachment/201103/25/2857981_1301041457imcC.jpg
http://hi.csdn.net/attachment/201103/25/2857981_1301041468UUu6.jpg
五.参考资料
《Simulink通信仿真教程》