{"id":22,"date":"2008-07-18T21:00:03","date_gmt":"2008-07-19T02:00:03","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/2008\/07\/18\/libraries-in-simulink\/"},"modified":"2016-12-20T19:08:23","modified_gmt":"2016-12-21T00:08:23","slug":"libraries-in-simulink","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2008\/07\/18\/libraries-in-simulink\/","title":{"rendered":"Libraries in Simulink"},"content":{"rendered":"<p>Have you ever noticed the same block constructs occurring\r\nrepeatedly in your model?\u00a0 Simulink libraries provide you with a way to capture\r\nthe template for an algorithm, and then reuse that template all over your\r\nmodel.\u00a0 For this post, I will to introduce the basic concepts of libraries,\r\nlibrary blocks, and library links.<\/p>\r\n\r\n<p><strong>Creating a library<\/strong><\/p>\r\n\r\n<p>To create a Simulink library, you have to create a new\r\nSimulink library from the File&gt; New &gt; Library menu, or using the <a\r\nhref=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/new_system.html\">new_system\r\nfunction<\/a>.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q3\/NewLibrary.png\" alt=\"The New Library Menu\"><\/p>\r\n\r\n<p>Libraries are special MDL-files.\u00a0 When you look at a library,\r\nyou will notice that it does not have the simulation controls a normal model\r\nhas.\u00a0 The library is not a functional model; it is a palette of components.\u00a0\r\nHere is an example library that contains a block for doing variable saturation\r\nof a signal.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q3\/SatLibDiag.png\" alt=\"The saturation library diagram.\"><\/p>\r\n\r\n<p>The library can be constructed in the same way a model is\r\nconstructed, by dragging blocks into the library canvas and connecting\r\nsignals.\u00a0 The Saturation Dynamic block is just a subsystem that contains the\r\nalgorithm for dynamic saturation of the signal u:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q3\/SaturationDynamic_system.png\" alt=\"The Saturation Dynamic algorithm\"><\/p>\r\n\r\n<p><strong>Locking the library<\/strong><\/p>\r\n\r\n<p>When a library is first created, it is unlocked.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q3\/libUnLocked.png\" alt=\"Unlocked library\"><\/p>\r\n\r\n<p>This allows you to edit and build the components.\u00a0 Once you\r\nsave the library and close it, the library is locked.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q3\/libLocked.png\" alt=\"Locked library\"><\/p>\r\n\r\n<p>No changes are allowed without unlocking the library.\u00a0 This\r\ncan be done from Edit &gt; Unlock Library.\u00a0 Most people just drag a block a few\r\npixels to trigger Simulink to prompt you to unlock the library.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q3\/AttemptToModifyUnlock.png\" alt=\"Modify locked library message\"><\/p>\r\n\r\n<p><strong>Reference blocks and algorithm reuse<\/strong><\/p>\r\n\r\n<p>Just like the blocks from the Simulink libraries, you can\r\nadd the library blocks to any model you are working on.\u00a0 There is some\r\nterminology that goes along with library blocks.\u00a0 When you add the library block\r\nto your model, you are creating a reference\r\nblock.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q3\/createRefBlock.png\" alt=\"Create a reference block by dragging the library block into the model.\"><\/p>\r\n\r\n<p>The reference block is an instance of the library block, but\r\nthe contents are not stored in the model.\u00a0 A model using the reference block stores\r\nonly a library link, which holds the path to the library block.<\/p>\r\n\r\n<code>&gt;&gt; get_param('satModel\/Saturation Dynamic','ReferenceBlock')<br\/>\r\nans =<br\/>\r\nSatLib\/Saturation Dynamic<\/code>\r\n\r\n<p>Set the Format &gt; Library Link Display to User to see\r\nwhich blocks in your model are actually library links.\u00a0 This setting adds a\r\nlibrary link icon to the corner of your block.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q3\/linkDisplayUser.png\" alt=\"Format -> Library Link Display -> User\"><\/p>\r\n\r\n<p><strong>Adding your library to the browser<\/strong><\/p>\r\n\r\n<p>At this point, you might be asking how to add the library to\r\n    the library browser.\u00a0 The short answer is: <tt>&gt;&gt; doc slblocks<\/tt>. \u00a0I provided\r\nan example of this in the <a\r\nhref=\"https:\/\/blogs.mathworks.com\/seth\/2008\/03\/18\/a-historical-tour-of-the-library-browser\/\">historical\r\ntour of the library browser<\/a>.<\/p>\r\n\r\n<p><strong>What do you do with libraries?<\/strong><\/p>\r\n\r\n<p>Do you maintain your own libraries?\u00a0 How many components\r\ndoes a typical library contain?\u00a0 Do you share your work with other modelers?\u00a0\r\nPost your library to the file exchange, or leave a <a\r\nhref=\"https:\/\/blogs.mathworks.com\/seth\/?p=22&amp;#comment\">comment here<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Have you ever noticed the same block constructs occurring\r\nrepeatedly in your model?\u00a0 Simulink libraries provide you with a way to capture\r\nthe template for an algorithm, and then reuse that template... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2008\/07\/18\/libraries-in-simulink\/\">read more >><\/a><\/p>","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[441,45],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/22"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=22"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/22\/revisions"}],"predecessor-version":[{"id":6126,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/22\/revisions\/6126"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=22"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=22"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=22"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}