{"id":2642,"date":"2013-09-11T10:36:57","date_gmt":"2013-09-11T15:36:57","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/?p=2642"},"modified":"2017-04-27T09:03:09","modified_gmt":"2017-04-27T14:03:09","slug":"the-matlab-system-block","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2013\/09\/11\/the-matlab-system-block\/","title":{"rendered":"The MATLAB System Block"},"content":{"rendered":"<p>In R2013b, you will notice a new block in the Simulink Library Browser: The <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/matlabsystem.html\">MATLAB System block<\/a>.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q3\/library.png\" alt=\"MATLAB System block in Simulink Library\" \/><\/p>\r\n\r\n<p><strong>System Object<\/strong><\/p>\r\n\r\n<p>Before looking at the MATLAB System block, let's looks at the evolution of <a title=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/what-are-system-objects.html (link no longer works)\">System objects<\/a>.<\/p>\r\n\r\n<p>In R2010a, <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/dspblks\/rn\/bsa8qr3-1.html#bse5dku\">System objects were introduced<\/a> in the Signal Processing Blockset. In contrast to MATLAB functions, System objects are object-oriented implementations of algorithms that can automatically manage state information, data indexing, and buffering, which is particularly useful in digital signal processing.<\/p>\r\n\r\n<p>In R2011a, <a href=\"https:\/\/blogs.mathworks.com\/seth\/2011\/04\/15\/introducing-the-system-toolboxes\/\">System toolboxes were introduced<\/a>. Built on System objects, those toolboxes give you the possibility to design (in MATLAB or Simulink) signal processing algorithms suitable for fast simulation, real-time prototyping, and embedded implementation.<\/p>\r\n\r\n<p>Starting in R2011b, users can <a title=\"https:\/\/www.mathworks.com\/help\/simulink\/system-objects.html (link no longer works)\">create custom System objects<\/a> in MATLAB. To include those System objects in Simulink, the MATLAB Function block could be used, but it required some tricks (<a href=\"https:\/\/blogs.mathworks.com\/seth\/2011\/04\/15\/introducing-the-system-toolboxes\/\">as shown in this post<\/a>).<\/p>\r\n\r\n<p>In R2013b the MATLAB System block makes it very easy to use custom System objects in Simulink.<\/p>\r\n\r\n<p><strong>A Simple Example<\/strong><\/p>\r\n\r\n<p>To illustrate the concept of using a System object in MATLAB and Simulink, I made a <tt>timesTwo<\/tt> System object based on the <a title=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/define-basic-system-objects-1.html (link no longer works)\">AddOne<\/a> example included the documentation.<\/p>\r\n\r\n<p>For that, all I need is to create a new class file, which inherits from the <tt><a title=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/matlab.systemclass.html (link no longer works)\">matlab.System<\/a><\/tt> class. Then I put the equations for my block in the <a title=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/matlab.system.stepimpl.html (link no longer works)\"><tt>stepImpl<\/tt><\/a> method:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q3\/timesTwoCode.png\" alt=\"Simple System Object\" \/><\/p>\r\n\r\n<p>I can use this System object in MATLAB with just two lines of code:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q3\/timeTwoPrompt.png\" alt=\"Simple System Object used in MATLAB\" \/><\/p>\r\n\r\n<p>In Simulink, it is as simple a dragging the block, and specifying the name of the object:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q3\/sysObj2.gif\" alt=\"Simple System Object used in Simulink\" \/><\/p>\r\n\r\n<p><strong>Why do we need a MATLAB System block?<\/strong><\/p>\r\n\r\n<p>The previous example is too simple to highlight the power of the MATLAB System block. With System objects, you will find a large number of methods to setup the  properties of <a title=\"https:\/\/www.mathworks.com\/help\/simulink\/properties-and-states.html (link no longer works)\">states<\/a>, <a title=\"https:\/\/www.mathworks.com\/help\/simulink\/system-block-output-specifications.html (link no longer works)\">output signals<\/a> and input signals. You will even find methods to manage the MATLAB System block <a title=\"https:\/\/www.mathworks.com\/help\/simulink\/system-block-icon-and-dialog.html (link no longer works)\">icon and dialog<\/a>.<\/p>\r\n\r\n<p>In the next weeks, I will follow up with a series of posts on the MATLAB System block to highlight more of its capabilities. For today, I will leave you with an example of a unit delay. When using the code below in the MATLAB System block, the input is marked as non-directfeedthrough. This makes it possible to use this block in loop without creating an <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/simulating-dynamic-systems.html\">algebraic loop<\/a>.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q3\/complexSysObjExample.png\" alt=\"Unit Delay System Object\" \/><\/p>\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>The MATLAB System block has the advantages of the MATLAB Function block: it is authored using the MATLAB language and can generate code. In addition, it gives you control over many properties that you could only access through C and MATLAB S-function... and all that in a modern object-oriented style of programming.<\/p>\r\n\r\n<p>Try the MATLAB System block and let us know what you think by leaving a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=2642&#comment\">comment here<\/a>.<\/p>\r\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q3\/complexSysObjExample.png\" onError=\"this.style.display ='none';\" \/><\/div><p>In R2013b, you will notice a new block in the Simulink Library Browser: The MATLAB System block.\r\n\r\n\r\n\r\nSystem Object\r\n\r\nBefore looking at the MATLAB System block, let's looks at the evolution of... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2013\/09\/11\/the-matlab-system-block\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[73,16],"tags":[341],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/2642"}],"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\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=2642"}],"version-history":[{"count":33,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/2642\/revisions"}],"predecessor-version":[{"id":6515,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/2642\/revisions\/6515"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=2642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=2642"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=2642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}