加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

simulink中模块库的建立和维护

(2014-01-31 05:49:21)
分类: Matlab



一.模块库的建立与修改

    模块库的建立与模型model的建立类似,都是在File菜单栏中,只不过要选择Library而非Model,如下图:

http://hi.csdn.net/attachment/201103/25/2857981_1301041389kwCK.jpg

    然后再该Library中添加了两个模块,并存为名为Interpolation.mdl文件。如下图:

http://hi.csdn.net/attachment/201103/25/2857981_1301041397jmC5.jpg

 

 

 

 

注意:当模块库被保存后,模块库就自动被锁定,模块库中的模块都无法修改,所以修改时需要Edit->Unlock Library来解锁方能修改。

 

 

 

 

二.模块库中建子模块库

     此处的目的是在HiNOC模块库中建立Interpolation子模块库(上一步已经建好了主体)。

  • 首先建立一个模块库保存为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.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文件的写法),此处的写法如下:

 

[c-sharp] view plaincopy
  1. function blkStruct slblocks  
  2. %SLBLOCKS Defines the block library for specific Toolbox or Blockset.  
  3.   SLBLOCKS returns information about Blockset to Simulink.  The  
  4.   information returned is in the form of BlocksetStruct with the  
  5.   following fields:  
  6.  
  7.     Name         Name of the Blockset in the Simulink block library  
  8.                  Blocksets Toolboxes subsystem.  
  9.     OpenFcn      MATLAB expression (function) to call when you  
  10.                  double-click on the block in the Blocksets Toolboxes  
  11.                  subsystem.  
  12.     MaskDisplay  Optional field that specifies the Mask Display commands  
  13.                  to use for the block in the Blocksets Toolboxes  
  14.                  subsystem.  
  15.     Browser      Array of Simulink Library Browser structures, described  
  16.                  below.  
  17.  
  18.   The Simulink Library Browser needs to know which libraries in your  
  19.   Blockset it should show, and what names to give them.  To provide  
  20.   this information, define an array of Browser data structures with one  
  21.   array element for each library to display in the Simulink Library  
  22.   Browser.  Each array element has two fields:  
  23.  
  24.     Library      File name of the library (mdl-file) to include in the  
  25.                  Library Browser.  
  26.     Name         Name displayed for the library in the Library Browser  
  27.                  window.  Note that the Name is not required to be the  
  28.                  same as the mdl-file name.  
  29.  
  30.   Example:  
  31.  
  32.       
  33.      Define the BlocksetStruct for the Simulink block libraries  
  34.      Only simulink_extras shows up in Blocksets Toolboxes  
  35.       
  36.      blkStruct.Name        ['Simulink' sprintf('/n''Extras'];  
  37.      blkStruct.OpenFcn     'simulink_extras' 
  38.      blkStruct.MaskDisplay sprintf('Simulink/nExtras');  
  39.  
  40.       
  41.      Both simulink and simulink_extras show up in the Library Browser.  
  42.       
  43.      blkStruct.Browser(1).Library 'simulink' 
  44.      blkStruct.Browser(1).Name    'Simulink' 
  45.      blkStruct.Browser(2).Library 'simulink_extras' 
  46.      blkStruct.Browser(2).Name    'Simulink Extras' 
  47.  
  48.   Copyright 1990-2006 The MathWorks, Inc.  
  49.   $Revision: 1.20.2.10  
  50.  
  51. Name of the subsystem which will show up in the Simulink Blocksets  
  52. and Toolboxes subsystem.  
  53.  
  54. blkStruct.Name ['Simulink' sprintf('/n''HiNOC'];  
  55.  
  56. The function that will be called when the user double-clicks on  
  57. this icon.  
  58.  
  59. blkStruct.OpenFcn 'HiNOC' 
  60.  
  61. The argument to be set as the Mask Display for the subsystem.  You  
  62. may comment this line out if no specific mask is desired.  
  63. Example:  blkStruct.MaskDisplay 'plot([0:2*pi],sin([0:2*pi]));' 
  64. No display for Simulink Extras.  
  65.  
  66. blkStruct.MaskInitialization '' 
  67. exp(j*[-45:-8:-215, -45]/180*pi);  
  68. x1 20 20 j*35;  
  69. x2 -x*10 60 +j*75;  
  70. p_str ['plot(',...  
  71.           mat2str(real(x1),2), ','mat2str(imag(x1),2), ','...  
  72.          '[0 15 'mat2str(real(x1(10:13)),2),0],[0 'mat2str(imag(x1(10:13)),2),0],'...  
  73.          mat2str(real(x2),2), ','mat2str(imag(x2),2), ','...  
  74.          '[19 40 35 52 40 49 60],[34 55 65 50 70 64 75],'...  
  75.          '[74.5 70 65 74],[84.5 80 85 94],',...  
  76.          '[66 65 70.5 71], [86 99 97 91],',...  
  77.          '[75 74 79 80 75], [81 94 92 79 81],',...  
  78.          '[74.5 73], [87 87],',...  
  79.          '-10, 0, 100, 100);'];  
  80. blkStruct.MaskDisplay p_str;  
  81.  
  82. Define the Browser structure array, the first element contains the  
  83. information for the Simulink block library and the second for the  
  84. Simulink Extras block library.  
  85.  
  86. Browser(1).Library 'simulink' 
  87. Browser(1).Name    'Simulink' 
  88. Browser(1).IsFlat  0;% Is this library "flat" (i.e. no subsystems)?  
  89. Browser(1).Library 'HiNOC' 
  90. Browser(1).Name    'HiNOC by dfd1r' 
  91. Browser(1).IsFlat  0;% Is this library "flat" (i.e. no subsystems)?  
  92. blkStruct.Browser Browser;  
  93. clear Browser;  
  94.  
  95. Define information about Signal Viewers  
  96.  
  97. Viewer(1).Library 'simviewers' 
  98. Viewer(1).Name    'Simulink' 
  99.   
  100. blkStruct.Viewer Viewer;  
  101. clear Viewer;  
  102.  
  103. Define information about Signal Generators  
  104.  
  105. Generator(1).Library 'simgens' 
  106. Generator(1).Name    'Simulink' 
  107.   
  108. blkStruct.Generator Generator;  
  109. clear Generator;  
  110. Define information for model updater  
  111. %blkStruct.ModelUpdaterMethods.fhDetermineBrokenLinks @UpdateSimulinkBrokenLinksMappingHelper;  
  112. blkStruct.ModelUpdaterMethods.fhSeparatedChecks  
  113. @UpdateSimulinkBlocksHelper;  
  114. End of slblocks  

      

      如此,就可在simulink的模块库列表中查看到自定义的模块库了,如下图:

http://hi.csdn.net/attachment/201103/25/2857981_130104144133y8.jpg

 

 


四.模块的引用的一些问题

     用户从模块库中复制模块到自己的模型中是对模块库中原型的引用,这种被复制到模型中的模块被称为引用块,二者之间存在关联,即如果库中的原始块被修改,则模型中的引用块也将被修改。

    判断一个模块是否是引用块,可以选择Format->Library Link Display->All,然后观察模块左下角是否有箭头标志(关联模块有箭头标志),如下图就是关联模块:

http://hi.csdn.net/attachment/201103/25/2857981_1301041457imcC.jpg

   若要对模型中的引用块进行修改,可以先取消关联关系,再修改,具体如下:

     1. 选中引用块,右键菜单Link Options->Disable Link来取消关联;

     2. 此时修改引用块,不会影响到原始块;

     3. 此时若要恢复关联,同样右键单击模块,选择右键菜单Link Options->Resolve Link,若果模块进行了修改,就会弹出如下提框:

http://hi.csdn.net/attachment/201103/25/2857981_1301041468UUu6.jpg

   4. 如果Action中选择push,则会更新库中的原始块以与当前模块相同,完成关联;如果Action中选择Restore,则会更新当前模块与原始块一致,完成关联。

   5. 如果要取当前块的关联彻底与模块库断开连接,断开后将无法恢复关联,使用右键Link Options->Break Link。

 

   若Simulink不能按照参考模块的连接找到模块库,则此参考模块会以红色的虚线框显示并给出错误信息,解决这个问题有以下两种方法:

       1.  删除此模块在重新设置新的模块;

       2.  用鼠标左键双击此模块,在弹出的对话框中填入正确的模块路径。

 

 

五.参考资料

 matlab 2010版的help文档

《Simulink通信仿真教程》

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有