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通訊模擬教程》