{"id":2724,"date":"2013-10-24T09:40:01","date_gmt":"2013-10-24T14:40:01","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/?p=2724"},"modified":"2013-10-24T09:40:01","modified_gmt":"2013-10-24T14:40:01","slug":"methods-of-the-matlab-system-block","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2013\/10\/24\/methods-of-the-matlab-system-block\/","title":{"rendered":"Methods of the MATLAB System Block"},"content":{"rendered":"<p>In the last <a href=\"https:\/\/blogs.mathworks.com\/seth\/2013\/09\/11\/the-matlab-system-block\">post on MATLAB System block<\/a>, I tried to provide a basic introduction to <a title=\"https:\/\/www.mathworks.com\/help\/simulink\/system-objects.html (link no longer works)\">System Objects in MATLAB<\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/matlab-system-block.html\">Simulink<\/a> environments. At the end, I finished with a quick example illustrating that System objects offer several methods to set up the properties of states and output signals of the system developed.<\/p>\r\n\r\n<p>This week, guest blogger Revathi Dukkipati will help us get a closer look at <a title=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/system-objects-methods.html (link no longer works)\">System object methods<\/a> and the sequence in which they are implemented.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q3\/rdukkipa_tn_large.jpg\" alt=\"Guest Blogger Revathi Dukkipati\" \/><\/p>\r\n\r\n<p><strong>Timing of methods in MATLAB<\/strong><\/p>\r\n\r\n<p>When authoring System objects, one important thing to realize is that there are methods that you can call (<em>for example <tt>step<\/tt> to process data<\/em>), and the methods that you implement (<em>for example <tt>stepImpl<\/tt> when you write your algorithm<\/em>). \r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q3\/setpVSstepImpl.png\" alt=\"step, versus stepImpl\" \/><\/p>\r\n\r\n<p>To understand why there is a difference between the name of the method you call and the name of the method you implement, I recommend looking at the <a title=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/methods-timing.html (link no longer works)\">Methods Timing<\/a> documentation page. You will find images like the following illustrating which methods you can call, and which methods you can or need to implement.\r\n\r\n<p>For the <tt>step<\/tt> method, this looks like:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q3\/authoring_step_call_seq.png\" alt=\"Timing of System objects methods\" \/><\/p>\r\n\r\n<p>The methods shown with a green background can be implemented by the user. Except for <tt>stepImpl<\/tt> which implements the algorithm, the other methods in green have an internal default implementation. You only need to implement them if the default is not appropriate for your application. The methods shown in white are completely controlled by MATLAB.<\/p>\r\n\r\n<p><strong>Timing of Methods in Simulink<\/strong><\/p>\r\n\r\n<p>When using a System object in Simulink through the MATLAB System block, it is important to know which methods are called during edit time, which methods are called during the model compilation and which methods are called every time step when the simulation runs.<\/p>\r\n\r\n<p> You can find an overview of this timing in the documentation page <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/simulink-engine-interaction-with-system-objects.html\">Simulink Engine Phases Mapped to System Object Methods<\/a>:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q3\/methodsTimingInSimulink.png\" alt=\"Timing of System objects methods\" \/><\/p>\r\n\r\n<p>Let's look at those phases in more details with example.:<\/p>\r\n\r\n<p><strong>Model Edit phase:<\/strong> This is where the object is created. Methods like <a title=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/matlab.system.getnumoutputsimpl.html (link no longer works)\"><tt>getNumOutputsImpl<\/tt><\/a> and <a title=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/matlab.system.getnuminputsimpl.html (link no longer works)\"><tt>getNumInputsImpl<\/tt><\/a> need to be called for the Simulink editor to know the number of outputs and inputs of the block. Those methods are optional, you need to implement them only if your block has more than one input port and one output port.<\/p>\r\n\r\n<p>Here is an example System object with two inputs and three outputs:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q3\/setNumOfIO.png\" alt=\"Example System Object with two inputs and three outputs\" \/><\/p>\r\n\r\n<p><strong>Model <a href=\"#f7-8245\">Compile phase<\/a>:<\/strong> When simulating a model, before the simulation can start, Simulink needs to determine the dimensions and other properties of all signals. For the properties of its input ports, the MATLAB system block inherits them from the blocks connected to it. For the output ports, the Simulink engine can try to infer them from the input ports, or call the <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/propagate-mixins.html#btzpvfw\">propagation methods<\/a> of your System object.<\/p> \r\n\r\nThese methods are inherited from the base class <a href=\"\"><tt>matlab.system.mixin.Propagates<\/tt><\/a> and required when the output specifications <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/propagate-mixins.html#bt2fty1\">cannot be inferred directly from the inputs<\/a>. Even when output specifications can be inferred, adding the Propagation methods can improve the compilation time of your model since the time spent on inferring this data is saved.<\/p>\r\n\r\n<p>Here is an example where the output has a dimension of five:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q3\/setOutDim.png\" alt=\"Example System Object with an output of dimension 5.\" \/><\/p>\r\n\r\n<p><strong>Model Execution phase:<\/strong> Once the model initialization is completed, the algorithm in <a title=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/matlab.system.stepimpl.html (link no longer works)\"><tt>stepImpl<\/tt><\/a> is called at every step. If the block contains states, I recommend separating the <tt>stepImpl<\/tt> into two functions: <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/matlab.system.mixin.nondirect.outputimpl.html\"><tt>outputImpl<\/tt><\/a> where you compute the values of output signals, and <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/matlab.system.mixin.nondirect.updateimpl.html\"><tt>updateImpl<\/tt><\/a> where you update the values of states internal to the block. Those two methods are provided by <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/matlab.system.mixin.nondirectclass.html\"><tt>matlab.system.mixin.Nondirect<\/tt><\/a> class.<\/p>\r\n\r\n<p>Here is an example implementing an accumulator, using <tt>outputImpl<\/tt> and <tt>updateImpl<\/tt><\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q3\/accumulate.png\" alt=\"Example System Object using outputImpl and updateImpl to implement an accumulator.\" \/><\/p>\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\nHave you tried the MATLAB System block?  Let us know what you think of the MATLAB System block by leaving a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=2724&#comment\">comment here<\/a>.\r\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q3\/accumulate.png\" onError=\"this.style.display ='none';\" \/><\/div><p>In the last post on MATLAB System block, I tried to provide a basic introduction to System Objects in MATLAB and Simulink environments. At the end, I finished with a quick example illustrating that... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2013\/10\/24\/methods-of-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":[87,16],"tags":[347,346],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/2724"}],"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=2724"}],"version-history":[{"count":45,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/2724\/revisions"}],"predecessor-version":[{"id":2895,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/2724\/revisions\/2895"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=2724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=2724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=2724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}