{"id":2692,"date":"2011-03-18T13:01:03","date_gmt":"2011-03-18T13:01:03","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2011\/03\/18\/hgsetgetplus-an-extension-to-hgsetget\/"},"modified":"2016-11-11T05:33:38","modified_gmt":"2016-11-11T10:33:38","slug":"hgsetgetplus-an-extension-to-hgsetget","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2011\/03\/18\/hgsetgetplus-an-extension-to-hgsetget\/","title":{"rendered":"HGSETGETPLUS &#8211; an extension to HGSETGET"},"content":{"rendered":"<style type=\"text\/css\">\r\ndiv.italic {\r\nfont-style: italic;\r\n}\r\ndiv.quote {\r\nbackground-color: #EEEEEE;\r\nfont-style: italic;\r\n}\r\ndiv.mlcode {\r\nwhite-space: pre;\r\nfont-family: \"Courier New\";\r\ncolor: black;\r\n}\r\nstring {\r\nwhite-space: pre;\r\nfont-family: \"Courier New\";\r\ncolor: #A020F0;\r\n}\r\ndiv.error {\r\nwhite-space: pre;\r\nfont-family: \"Courier New\";\r\ncolor: red;\r\n}\r\ndiv.comment {\r\nwhite-space: pre;\r\nfont-family: \"Courier New\";\r\ncolor: #228B22;\r\n}\r\n<\/style>\r\n\r\n<a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/31289\">Eric<\/a>\u2019s pick this week is \u201c<a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/30713-handlegraphicssetget-class\">HandleGraphicsSetGet<\/a>\u201d by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/73465\">Andrew Newell<\/a>.<br><br>\r\n<div class=\"italic\">Hello, my name is Eric, and I\u2019ll be contributing to the Pick of the Week blog from time to time. This week is one such occasion! Like Jiro and Brett, I\u2019m an Application Engineer here at MathWorks. I started using MATLAB in grad school for data filtering and statistics, and pretty soon I started looking for any reason I could to use it!<\/div><br>\r\n<p>\r\nOne of my favorite features in MATLAB is the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/matlab_oop\/ug_intropage.html\">object-oriented programming (OOP) system<\/a>. It\u2019s great for building complex GUIs, creating components to share with other people, and for managing large projects. A particularly useful feature of the OOP system in MATLAB is the ability to create classes that behave like <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/creating_plots\/f7-20419.html\">Handle Graphics<\/a> objects, with a <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/set.html\">SET<\/a>\/<a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/get.html\">GET<\/a> interface. To do this, you write a class that <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/matlab_oop\/brdf7kt.html\">inherits<\/a> from \u201c<a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/hgsetget.html\">hgsetget<\/a>\u201d, a built-in MATLAB class.\r\n<p>\r\nThis week\u2019s pick is a very useful extension to the hgsetget class that started out as a MATLAB Answers post by Andrew. Using inheritance, Andrew wanted to extend the hgsetget class to add an easy validation and enumeration interface for property values. Jiro shared some more of the background story:\r\n\r\n<div class=\"quote\">\u201cIn case you were interested, Andrew wrote this entry in response to this <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/answers\/1998-implementing-an-hgsetget-subclass\">MATLAB Answers question<\/a> he posted a few weeks ago. When he mentioned he came up with a good solution, I encouraged him to post it on File Exchange. I\u2019m glad he did, because it\u2019s a great example of building on top of a base MATLAB class.\u201d<\/div><br>\r\n<p>\r\nAfter some dialog and feedback from Jiro, the end result was a new MATLAB class called \u201chgsetgetplus\u201d. It allows you to easily constrain the set of values that properties in your class can assume, and to notify the user what those values are. To see how this can be useful, consider the behavior of built-in Handle Graphics objects in MATLAB: \r\n<div class=\"mlcode\">  >> h = surf(peaks);\r\n  >> set(h, <string>'LineWidth'<\/string>, -5);<\/div>\r\n<div class=\"error\">  ??? Error using ==> set\r\n  Value must be finite and greater than zero<\/div><br>\r\n<p>\r\nYou can certainly implement behavior like this in your own classes. However, you must write a \u201cset\u201d method for each property, and include tedious validation code to produce the error. Andrew\u2019s \u201chgsetgetplus\u201d class makes this easy. When you write your own classes, you simply inherit from the \u201chgsetgetplus\u201d class, and define the attributes of your class\u2019s properties with this simple clean interface: \r\n<div class=\"comment\">  % Code that sets property constraints for a hypothetical\r\n  % 'LineStyle' class. This code is inside of the CLASSDEF file.<\/div>\r\n<div class=\"mlcode\">  obj.width.classes = {<string>'numeric'<\/string>};\r\n  obj.width.attributes = {<string>'integer'<\/string>, <string>'>'<\/string>, 0};\r\n  obj.style.classes = {<string>'char'<\/string>};\r\n  obj.style.choices = {<string>'solid'<\/string>, <string>'dotted'<\/string>, <string>'dashed'<\/string>};<\/div><br>\r\n<p>\r\nThis is what happens when we manipulate an object from the above class: \r\n<div class=\"comment\">  % Code that attempts to set the 'width' property of an\r\n  % object from the above class.<\/div>\r\n<div class=\"mlcode\">  >> set(obj, <string>'width'<\/string>, -5);<\/div>\r\n<div class=\"error\">  ??? Error using ==> setOneProperty\r\n  Expected width to be an array with all of the values > 0.<\/div><br>\r\n<div class=\"comment\">  % Enumerates valid values for the 'style' property.<\/div>\r\n<div class=\"mlcode\">  >> set(obj, <string>'style'<\/string>)\r\n  [ solid | dotted | dashed ]<\/div><br>\r\n<p>\r\nAndrew\u2019s submission includes an example object and script that you can run to see behavior similar to the example above. \r\n<p>\r\nI like this submission for several reasons:<ol>\r\n<li>It increases your productivity as a MATLAB programmer.<\/li>\r\n<li>It makes your code more reliable by reducing a common, and difficult to find, source of bugs.<\/li>\r\n<li>In addition to displaying a helpful error message, it also allows users to view the universe of valid property values, thus avoiding the error message in the first place, and improving their self esteem.<\/li>\r\n<li>Back when I helped staff our technical support line, somebody called and requested that we add this exact feature!<\/li>\r\n<li>It\u2019s a great example of how to arrange and package a MATLAB utility. It includes clear documentation in HTML format, example code, and even unit tests that can be run with <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/22204\">Steve Eddins<\/a>\u2019s xUnit framework. The content is grouped into separate folders, keeping things organized. If you find yourself developing tools in MATLAB for others to use, this is a good illustration of how you can bundle everything up cleanly.<\/li>   \r\n<\/ol>\r\n<p>\r\n<strong>Comment<\/strong>\r\n<p>\r\nLet us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=2692#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/30713-handlegraphicssetget-class#comments\">comment<\/a> for Andrew. ","protected":false},"excerpt":{"rendered":"<p>\r\ndiv.italic {\r\nfont-style: italic;\r\n}\r\ndiv.quote {\r\nbackground-color: #EEEEEE;\r\nfont-style: italic;\r\n}\r\ndiv.mlcode {\r\nwhite-space: pre;\r\nfont-family: \"Courier New\";\r\ncolor: black;\r\n}\r\nstring... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2011\/03\/18\/hgsetgetplus-an-extension-to-hgsetget\/\">read more >><\/a><\/p>","protected":false},"author":36,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2692"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/36"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=2692"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2692\/revisions"}],"predecessor-version":[{"id":8034,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2692\/revisions\/8034"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2692"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2692"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2692"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}