数学建模社区-数学中国

标题: MATLAB 添加自定义的模块到simulink库浏览器 [打印本页]

作者: 逝水lcc无痕    时间: 2015-1-22 10:25
标题: MATLAB 添加自定义的模块到simulink库浏览器
安装了matlab2014b后,突然发现Simulink Library Browser窗口中没有内容,找了一种方法添加进去,希望对有需要的朋友有所帮助!
4 d$ r, }7 c' J/ U# y( }9 X1、创建库文件:打开Simulink Library Browser窗口。要建立Simulink库文件,首先启动Simulink工作窗口,单击菜单栏【File】下的【New】选项,选择【library】选项, 打开一个新的Library窗口界面,此时,用户可以将自己需要添加的一些模块加入到新的窗口中。然后保存为Own_efinition.mdl(所需要定义的库文件名称)。这样,就建立了一个 自定义的库文件。 & d! N5 M, ~* y0 J7 y4 l: O
2、在Matlab路径下创建存放库文件的路径,也即自定义的模块库在Library Browser下的显示位置。注意在Matlab中,每一个模块库文件所在的路径必须不同。
$ f8 T! S3 W0 B+ l- ]) v/ N' J2 e( o创建的Maltab路径是:Drogram FilesMATLABR2011btoolboxSimulinkSimulinkMyLibrary(根据自己的Matlab安装路径来确定),其中MyLibrary为自定义的文件夹。
# R3 h( P+ t+ V6 g1 t' @( H7 N3、将第一步中建立的库文件拷贝到新建的Matlab路径下。在Matlab主窗口的【File】菜单栏下选择【Set Path】选项,然后单击【Add Folder】按钮,将新建的 路径添加进来,然后保存(Save),退出(Close)。
# o* G8 X( `8 e3 _& g/ H: c! }8 z4、要显示自定义的模块库,还需要拷贝slblocks.m函数到新建的路径下。在Matlab的命令窗口中输入:
, j* d" I. {( f, o- d* `: M>> which('slblocks.m', '-all') . ]* ~+ [5 R# \) z# k! U' u$ r0 h  P
>> open('Drogram FilesMATLABR2011btoolboxSimulinkSimulinkblocksslblocks.m')  7 u9 A4 [; @: J7 }0 P( ~3 T
0 A2 c: u, X* M5 V' s$ j
这样就可以打开slbocks.m文件模板,为了将自定义的模块库显示在Library Browser窗口下,需要对该程序进行修改。首先把slbocks.m文件拷贝到自定义库文件
! ^3 n& k; o, C" E. ?. d9 _' r同一目录下(即刚才创建的路径),然后打开该文件进行如下修改(红色为所作修改),并保存: 4 C) d1 r# Z: H- z3 X5 H4 w5 j. i5 q
function blkStruct = slblocks - W  v6 O$ p/ K8 P6 p# i3 P
%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: %
/ y( M; ]. Z2 L3 s! D& Q% Name Name of the Blockset in the Simulink block library % Blocksets & Toolboxes subsystem.
: t7 B1 W( u/ |, N$ t% OpenFcn MATLAB expression_r(function) to call when you % double-click on the block in the Blocksets & Toolboxes % subsystem.
8 O/ F% O$ [# B4 R6 F7 G% MaskDisplay Optional field that specifies the Mask Display commands % to use for the block in the Blocksets & Toolboxes % subsystem. . m8 A: B! o; S# D! |
% Browser Array of Simulink Library Browser structures, described % below.
' G: ?! f& s/ a4 U" `6 g5 F$ n/ K
( u# L& @/ \! e) Z: l* K
; [, H/ G& ?" J! X% N
; I* }7 u1 {0 I- @4 M" d6 y4 a' }, j( `
) {* _& l: J& q9 C  e# m* u, K! u+ r1 M! Z5 S
, d3 Y2 {9 Q2 S- ~& Z. s7 K, Q
9 B+ Q/ _4 Q+ T' N( G, j

; Z* `& k0 }( D0 ^0 S2 ?% 8 `% p$ x4 f9 N; H  [: g
% 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: % , X$ Z1 O; z$ k
% Library File name of the library (mdl-file) to include in the % Library Browser.
: v, ^4 }: z: X% 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. % : h( o4 H' Y- N" d! x  i/ K
% Example: % % % 1 V+ q- V; ]# f/ F
% % Define the BlocksetStruct for the Simulink block libraries % % Only simulink_extras shows up in Blocksets & Toolboxes % %
* ]$ h( S; o$ b) e  M+ p0 M$ Y% blkStruct.Name = ['Simulink' sprintf('n') 'Extras']; % blkStruct.OpenFcn = 'simulink_extras';
' ^# p* A4 Q# A+ R* p% r: S4 w% blkStruct.MaskDisplay = sprintf('SimulinknExtras'); % % %
4 C! ^7 Q# q! ]% % Both simulink and simulink_extras show up in the Library Browser. % % . D7 {$ ~. i0 @; M4 y
% blkStruct.Browser(1).Library = 'simulink'; % blkStruct.Browser(1).Name = 'Simulink'; % blkStruct.Browser(2).Library = 'simulink_extras'; % blkStruct.Browser(2).Name = 'Simulink Extras'; % ' P) p3 }" `: J" S0 O& C
% Copyright 1990-2006 The MathWorks, Inc. % $Revision: 1.20.2.10 $ % & C  J( }" l  E; e
% Name of the subsystem which will show up in the Simulink Blocksets
8 s: S% H( L; x1 t2 |6 t4 F
$ x% w+ X: v6 b" A' ^; e
7 ^+ e# d/ f6 ^3 }8 M
  p. P7 J5 I0 g2 v* h% and Toolboxes subsystem. % 3 d, F- [1 Q" \  A: H
blkStruct.Name = ['Simulink' sprintf('n') 'Extras']; % 8 ~1 F# Q3 S/ L( Y, A
% The function that will be called when the user double-clicks on % this icon. %
2 y& L) d. `  a, yblkStruct.OpenFcn = 'simulink_extras'; %
( \7 z: s8 f' Q# B% 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. % ! `2 b6 ~" C- x; o) U( f
blkStruct.MaskDisplay = ''; %
: q+ i1 |0 J2 J' U# l, E% 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. %
  H- \6 a" S; x( eBrowser(1).Library = 'Own_efinition'; %这个是库文件名
# d& G8 c8 C: Z* iBrowser(1).Name = 'MyLibrary'; %这个是需要在库浏览器中显示的名称 Browser(1).IsFlat = 0;% Is this library "flat" (i.e. no subsystems)? Browser(2).Library = 'simulink_extras'; Browser(2).Name = 'Simulink Extras';
+ S0 h  z1 b2 y6 ^Browser(2).IsFlat = 0;% Is this library "flat" (i.e. no subsystems)? blkStruct.Browser = Browser; clear Browser; %
5 t' T2 Y0 ?5 ]% Define information about Signal Viewers % 9 ^; B0 c- X1 l0 ^+ x. y! s& X. P0 N
Viewer(1).Library = 'simviewers'; Viewer(1).Name = 'Simulink'; blkStruct.Viewer = Viewer; clear Viewer; : S* L  J) g+ [# L& @! X- B

" I* x$ @" U9 j* P 3 w$ c, ~- I7 r, s5 Y, O' y
%
: z+ J6 o: j- E: \  l/ y1 o% Define information about Signal Generators % ( S4 ]* i0 |8 ~1 ?' P
Generator(1).Library = 'simgens'; Generator(1).Name = 'Simulink'; blkStruct.Generator = Generator; clear Generator; ( J  O. ^2 @5 v0 g* k" ]# Z
% Define information for model updater
) z; |/ N1 A4 x2 c%blkStruct.ModelUpdaterMethods.fhDetermineBrokenLinks = @UpdateSimulinkBrokenLinksMappingHelper;
# M) t5 I( v( p$ b! F8 @4 k, e0 [blkStruct.ModelUpdaterMethods.fhSeparatedChecks = @UpdateSimulinkBlocksHelper; % End of slblocks ) [! W( u7 ~. A3 V* K4 C. E
5、再打开Simulink Library Browser,按F5刷新,即可看到自定义的模块库,展开后可以看到内部的自定义模块。那么用户可以方便地进行拖放模块,就可以使用 自定义的封装模块了。
9 T; b0 G* e0 t- q) H' N7 G) w* G如果还要在新建的模块库下面创建子模块库,需要把Subsystem模块加入到模块库中,打开Subsystem模块,删除所有内容(ports等元件),添加自己创建的模块
+ `' r3 `& \; x  a( R6 J) F保存即可,然后重新打开Simulink Library Browser,按F5刷新,即可看到自定义的模块库和字模块库。7 y* x" u" p) L5 q

0 s6 D) t/ F0 @" |  K$ ~- i( a. @
( v( ?8 q/ a1 f# E* h9 |




欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5