S-Function简单封装
对于S-Function,这里做一个简单的封装。直接将模块的本来面目暴露出来,实在有点low,所以封装一下😀
模块上右键选择Mask->Create Mask…
函数比较简单,没有参数,所以这里只是设定名字,输入端口名。
然后把模块下的S-Function也改了吧。
新建Library
在Simulink中新建Library。
将前面封装的S-Function拖放到新建的library文件中,也可以随便增加其他模块。这里模仿Help,增加了两个subsystem。
其中一个subsystem使用了“OpenFcn"的回调函数,可以指向另一个library。
在命令窗口键入
set_param(gcs,'EnableLBRepository','on');
这样刷新Simulink库时,就会出现自定义库,否则需要手动设置后显示。
然后新建另一个library,一样根据help文档添加了一个annotation,并设置了其描述。
将上述library保存,并将其路径添加到Matlab的搜索路径中。
编写slblocks.m
在Matlab中选择New File > Script,新建脚本,命名为slblocks.m
function blkStruct = slblocks
% This function specifies that the library should appear
% in the Library Browser
% and be cached in the browser repository
Browser.Library = 'mylib';
% 'mylib' is the name of the library
Browser.Name = 'My Library';
% 'My Library' is the library name that appears
% in the Library Browser
blkStruct.Browser = Browser;
根据实际情况,更改上述代码。
刷新library
回到Simulink library browser中右键选择Refresh Library Browser,或直接F5,可见添加的自定义库。
使用sl_customization.m文件可以更改库显示的次序。默认均为0,Simulink为-1。
function sl_customization(cm)
% Change the order of libraries in the Simulink Library Browser.
cm.LibraryBrowserCustomizer.applyOrder({'Msh IO Library',-2});
end
这里可以将自定义库添加到Browser的顶端。
引用
Matlab help文档,搜索Add Libraries to the Library Browser可见
另,参考博客:
[1]: Simulink如何添加模块到Library Browser