{"id":5438,"date":"2016-04-26T16:32:28","date_gmt":"2016-04-26T21:32:28","guid":{"rendered":"https:\/\/blogs.mathworks.com\/simulink\/?p=5438"},"modified":"2016-04-26T16:32:28","modified_gmt":"2016-04-26T21:32:28","slug":"system-object-editor-enhancements","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2016\/04\/26\/system-object-editor-enhancements\/","title":{"rendered":"System Object Editor Enhancements"},"content":{"rendered":"<p><em>Today I am happy to welcome guest blogger Jacob Halbrooks to describe enhancements to the MATLAB Editor to help editing <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/define-new-system-objects.html\">System Objects<\/a>.<\/em><\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q1\/Jacob_Profile.jpg\" alt=\"Jacob Halbrooks, guest blogger\" \/><\/p>\r\n\r\n<p>In R2016a, MATLAB Editor includes new tools to help you write System objects. These tools appear when you create a new System object or open an existing one. Here, we'll use these tools to help us write a simple moving average filter for use in MATLAB or Simulink.<\/p>\r\n\r\n<p><strong>Enhanced MATLAB Editor<\/strong><\/p>\r\n\r\n<p>We start by creating a new System object in MATLAB. From the MATLAB Home tab, select <strong>New<\/strong> and then choose <strong>System Object > Basic<\/strong>. A new document is created with template code for a basic System object. In addition, observe the new <strong>System Object<\/strong> section of the toolstrip:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q2\/image1.png\" alt=\"Toolstrip\" \/><\/p>\r\n\r\n<p>This section includes tools to insert code, display a code outline (Analyze), and preview your System block dialog.<\/p>\r\n\r\n<p>Let's change the name of the System object to \"MovingAverageFilter\", update the class comment, and save it. This gives us a good outline to start from:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q2\/image2.png\" alt=\"Default System Object Template\" \/><\/p>\r\n\r\n<p><strong>Preview dialog<\/strong><\/p>\r\n\r\n<p>A major feature of the MATLAB System block is that it <a href=\"https:\/\/blogs.mathworks.com\/simulink\/2014\/01\/15\/a-free-dialog-for-your-matlab-system-block\/\">creates a free dialog for you<\/a>. In MATLAB Editor, you can now preview the block dialog before you even create a System block or open Simulink. Click the top of the <strong>System Block<\/strong> button, and you will see the dialog open:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q2\/image3.png\" alt=\"Dialog Preview\" \/><\/p>\r\n\r\n<p>You can keep this dialog open, and it will automatically update as you save changes to your System object.<\/p>\r\n\r\n<p><strong>Insert Properties and States<\/strong><\/p>\r\n\r\n<p>Our moving average filter should have a public property for the window length and a private property to hold a pre-computed constant, which is a value that is calculated once. In addition, the filter requires a discrete-state property. From the toolstrip, pull down the <strong>Insert Property<\/strong> menu. The menu lists the most common types of properties used in System objects as well as a custom option to let you define a property with your choice of attributes. Choose <strong>Numeric<\/strong> for the window length property:\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q2\/image4.png\" alt=\"Insert Property\" \/><\/p>\r\n\r\n<p>A new section of code is inserted for this type of property, and the name of the property is selected so you can easily type to rename it:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q2\/image5.png\" alt=\"Rename Property\" \/><\/p>\r\n\r\n<p>Rename the property and give it a default value and comments:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q2\/image6.png\" alt=\"Rename Property\" \/><\/p>\r\n\r\n<p>Save the file and note how the dialog updates:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q2\/image7.png\" alt=\"Updated dialog preview\" \/><\/p>\r\n\r\n<p>Similarly, add a private property from the <strong>Insert Property<\/strong> menu and name it \"pCoefficients\". The state can be added using the <strong>Insert State<\/strong> button on the toolstrip:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q2\/image8.png\" alt=\"Insert State\" \/><\/p>\r\n\r\n<p>Remove extra space and the unused properties code block, and the System object now looks like:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q2\/image9.png\" alt=\"Overall\" \/><\/p>\r\n\r\n<p><strong>Insert Methods<\/strong><\/p>\r\n\r\n<p>Now let's define the behavior of the System object by implementing its methods:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q2\/image10.png\" alt=\"Defining Methods\" \/><\/p>\r\n\r\n<p>While its algorithm has been implemented, we want an additional feature for our System object, which is to validate the input data. In order to discover a method to implement that feature, we can select the <strong>Insert Method<\/strong> button. This opens a gallery that lists the complete System object API:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q2\/image11.png\" alt=\"Insert Methods\" \/><\/p>\r\n\r\n<p>The gallery shows which methods are already implemented and allows you to browse other methods and select one for insertion. The \"Validate inputs\" method from the \"Inputs and Outputs\" group sounds like what we want, so select that. The following code is inserted:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q2\/image12.png\" alt=\"Validate Inputs\" \/><\/p>\r\n\r\n<p>Now we implement the method and the System object is complete. The final version is:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q2\/image13.png\" alt=\"Final filter\" \/><\/p>\r\n\r\n<p>The System object is now ready to use either in MATLAB or in Simulink using the <a href=\"https:\/\/blogs.mathworks.com\/simulink\/2013\/09\/11\/the-matlab-system-block\">MATLAB System block<\/a>!<\/p>\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>We hope this enhancement to the MATLAB Editor will make you life creating system objects easier. Give it a try and let us know what you think by leaving a <a href=\"https:\/\/blogs.mathworks.com\/simulink\/?p=5438&#comment\">comment here<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q2\/image13.png\" onError=\"this.style.display ='none';\" \/><\/div><p>Today I am happy to welcome guest blogger Jacob Halbrooks to describe enhancements to the MATLAB Editor to help editing System Objects.\r\n\r\n\r\n\r\nIn R2016a, MATLAB Editor includes new tools to help you... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2016\/04\/26\/system-object-editor-enhancements\/\">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":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/5438"}],"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=5438"}],"version-history":[{"count":16,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/5438\/revisions"}],"predecessor-version":[{"id":5454,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/5438\/revisions\/5454"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=5438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=5438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=5438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}