相信很多人在做项目时往往会遇到两个项目需要用到同样的simulink模块的问题,如果我们将自己的做过的一些项目的关键部分模块化后,将非常利于这些项目的可移植。接下来将说一说如何在simulink中将自己编好的模块放到浏览器中。
1.在Matlab文件夹中新建自己的库文件的路径,也即自定义的模块库在Library Browser下的显示位置。注意在Matlab中,每一个模块库文件所在的路径必须不同。
我创建的库文件路径是:"F:\matlab\toolbox\simulink\mylibrary"(根据自己的Matlab安装路径来确定),其中myLibrary为自定义的新文件夹。
2.选择你将要放到库里面的文件,在这里我已自己编写的一个齿轮箱模型为例,将模型保存至刚刚建立的库文件夹里,取名为gear_sub
3.在Matlab主窗口的菜单栏下选择【Set Path】设置路径这一选项,然后单击添加新的路径按钮,将新建的mylibrary路径添加进来,然后保存(Save),退出(Close)。
4.将下面这段代码放到mylibrary文件夹下,该段代码是为了让你自己的库文件显示在浏览器界面里面
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_r(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') 'Extras'];
%
% The function that will be called when the user double-clicks on
% this icon.
%
blkStruct.OpenFcn = 'simulink_extras';
%
% 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.MaskDisplay = '';
%
% 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 = 'gear_sub'; %这个是库文件名
Browser(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';
Browser(2).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
其中代码里面红色的两行是更改成你自己去的库的名字的。
最后进入浏览器,选择F5进行刷新,就能看到你自己的库文件了