{"id":916,"date":"2017-04-04T14:18:26","date_gmt":"2017-04-04T18:18:26","guid":{"rendered":"https:\/\/blogs.mathworks.com\/developer\/?p=916"},"modified":"2017-04-04T16:09:59","modified_gmt":"2017-04-04T20:09:59","slug":"properties-of-properties","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/developer\/2017\/04\/04\/properties-of-properties\/","title":{"rendered":"Properties of properties"},"content":{"rendered":"<div class=\"content\"><!--introduction--><p>A while ago we discussed a good reason to appreciate and use MATLAB's model for properties. It had to do with <a href=\"https:\/\/blogs.mathworks.com\/developer\/2016\/08\/08\/han-solo-encapsulation\/\">encapsulation if you remember<\/a>, and we discussed how MATLAB's properties are inherently encapsulated, preventing the need to write a bunch of boiler plate setters and getters.<\/p><p>There's more goodness where that came from! Another reason to use MATLAB's properties is to benefit directly from the fact that everything in MATLAB is considered an array that can be sliced and diced however you please.<\/p><p>The beauty if MATLAB is in its simplicity of handling arrays. Rather than requiring some external container like a <tt>List<\/tt> or a <tt>Vector<\/tt> of something, everything is inherently already a list and a vector! With this you can be free to vectorize to your heart's content, as well as index into your array using the powerful MATLAB syntax we all know and love.<\/p><p>This gets us back to object properties. If you use properties instead of setter and getter methods you benefit from all this indexing sugar for free.<\/p><!--\/introduction--><p><b>Pulling on strings<\/b><\/p><p>Case in point: Let's say we love the new string class in MATLAB. After all what's not to love? The new MATLAB strings are rational and array oriented, opening us to fast and powerful manipulation techniques. A quick point is that we can indeed leverage this \"arrayness\" directly in the string class itself without writing our own object code. For example, if you wanted to quickly do some manipulation on a given string in order to get your priorities straight you can do so quite easily. Let's start by creating a string scalar:<\/p><pre class=\"codeinput\">openingLine = <span class=\"string\">\"It was the best of times it was the worst of times\"<\/span>\r\n<\/pre><pre class=\"codeoutput\">\r\nopeningLine = \r\n\r\n    \"It was the best of times it was the worst of times\"\r\n\r\n<\/pre><p>This is great, we can operate on this as a single string rather than the old character vector approach where each character is an element of the array. However, right now, I'd like to operate on this as an array of words, so let's split it up:<\/p><pre class=\"codeinput\">openingLine = split(openingLine)\r\n<\/pre><pre class=\"codeoutput\">\r\nopeningLine = \r\n\r\n  12&times;1 string array\r\n\r\n    \"It\"\r\n    \"was\"\r\n    \"the\"\r\n    \"best\"\r\n    \"of\"\r\n    \"times\"\r\n    \"it\"\r\n    \"was\"\r\n    \"the\"\r\n    \"worst\"\r\n    \"of\"\r\n    \"times\"\r\n\r\n<\/pre><p>Now that this is an array of words, we can easily have it conform to our nefarious demands! We can replace \"times\" with \"blogs\" simply using the power of indexing:<\/p><pre class=\"codeinput\">openingLine(contains(openingLine, <span class=\"string\">\"times\"<\/span>)) = <span class=\"string\">\"blogs\"<\/span>\r\n<\/pre><pre class=\"codeoutput\">\r\nopeningLine = \r\n\r\n  12&times;1 string array\r\n\r\n    \"It\"\r\n    \"was\"\r\n    \"the\"\r\n    \"best\"\r\n    \"of\"\r\n    \"blogs\"\r\n    \"it\"\r\n    \"was\"\r\n    \"the\"\r\n    \"worst\"\r\n    \"of\"\r\n    \"blogs\"\r\n\r\n<\/pre><p>Note for MATLAB strings the <tt>replace<\/tt> function is actually easier than this, but I wanted to show the broader context of array indexing. In practice with strings we should use <tt>replace<\/tt>.<\/p><pre class=\"codeinput\">openingLine = <span class=\"string\">\"It was the best of times it was the worst of times\"<\/span>\r\nopeningLine = split(openingLine)\r\nopeningLine = replace(openingLine, <span class=\"string\">\"times\"<\/span>, <span class=\"string\">\"blogs\"<\/span>)\r\n<\/pre><pre class=\"codeoutput\">\r\nopeningLine = \r\n\r\n    \"It was the best of times it was the worst of times\"\r\n\r\n\r\nopeningLine = \r\n\r\n  12&times;1 string array\r\n\r\n    \"It\"\r\n    \"was\"\r\n    \"the\"\r\n    \"best\"\r\n    \"of\"\r\n    \"times\"\r\n    \"it\"\r\n    \"was\"\r\n    \"the\"\r\n    \"worst\"\r\n    \"of\"\r\n    \"times\"\r\n\r\n\r\nopeningLine = \r\n\r\n  12&times;1 string array\r\n\r\n    \"It\"\r\n    \"was\"\r\n    \"the\"\r\n    \"best\"\r\n    \"of\"\r\n    \"blogs\"\r\n    \"it\"\r\n    \"was\"\r\n    \"the\"\r\n    \"worst\"\r\n    \"of\"\r\n    \"blogs\"\r\n\r\n<\/pre><p><b>Make it your own<\/b><\/p><p>Alright, vectorization and indexing are powerful in the MATLAB environment, but how do we leverage this in our own objects? The answer is to use properties! Let's say we want to create our own <i>fancy<\/i> string class which adds to the capabilities of the string class we see in MATLAB. Perhaps we want to keep the underlying text content of the string pure an unchanged, but we want to add specific meta-data to individual elements. For example, in environments that support hyperlinks (like the MATLAB command window) I may want to add a hyperlink, but I don't want this hyperlink to modify the real text content. Maybe I'd like to print that to a text file and the hyperlink would get in the way. Let's look at how we can do this, and benefit from the indexing behavior of MATLAB's properties.<\/p><p>First, we should create our <tt>FancyString<\/tt> class. Note we leverage composition over inheritance (<a href=\"https:\/\/blogs.mathworks.com\/developer\/2015\/05\/01\/composition-over-inheritance\/\">remember<\/a>?) and we hold on to the \"plain\" string. However, the constructor passes all values through to the string constructor so that it feels like a normal string. Note we can also add converter methods for char, string, and cellstr so that we can combine these values together effortlessly.<\/p><pre class=\"language-matlab\">\r\n<span class=\"keyword\">classdef<\/span> FancyString \r\n    \r\n    <span class=\"keyword\">properties<\/span>\r\n        PlainValue\r\n    <span class=\"keyword\">end<\/span>\r\n\r\n    \r\n    <span class=\"keyword\">methods<\/span>\r\n        <span class=\"keyword\">function<\/span> fancyString = FancyString(varargin)\r\n            <span class=\"keyword\">if<\/span> nargin &lt; 1\r\n                <span class=\"comment\">% Supporting a zero argument constructor allows us to<\/span>\r\n                <span class=\"comment\">% preallocate. This is very powerful when creating arrays.<\/span>\r\n                <span class=\"comment\">% Note this constructor itself preallocates below.<\/span>\r\n                <span class=\"keyword\">return<\/span> \r\n            <span class=\"keyword\">end<\/span>\r\n                        \r\n            <span class=\"comment\">% Create the plain string<\/span>\r\n            plainValue = string(varargin{:}); \r\n            <span class=\"comment\">% Create the array of FancyStrings to match. <\/span>\r\n            fancyString(numel(plainValue)) = FancyString; \r\n\r\n            <span class=\"comment\">% Each element of this FancyString array holds onto (composes)<\/span>\r\n            <span class=\"comment\">% a regular string scalar. Send each element of the string<\/span>\r\n            <span class=\"comment\">% array to each element of the FancyString array<\/span>\r\n            plainValueCell = num2cell(plainValue);\r\n            [fancyString.PlainValue] = deal(plainValueCell{:});\r\n        <span class=\"keyword\">end<\/span>\r\n            \r\n        <span class=\"keyword\">function<\/span> c = char(fancyString) <span class=\"comment\">% Allow conversion from char<\/span>\r\n            c = char(fancyString.PlainValue);\r\n        <span class=\"keyword\">end<\/span>\r\n        <span class=\"keyword\">function<\/span> c = cellstr(fancyString) <span class=\"comment\">% Allow conversion from cellstrs<\/span>\r\n            c = cellstr({fancyString.PlainValue});\r\n        <span class=\"keyword\">end<\/span>\r\n        <span class=\"keyword\">function<\/span> s = string(fancyString) <span class=\"comment\">% Allow conversion from strings<\/span>\r\n            s = [fancyString.PlainValue];\r\n        <span class=\"keyword\">end<\/span>\r\n    <span class=\"keyword\">end<\/span>\r\n<span class=\"keyword\">end<\/span>\r\n\r\n<\/pre><p>Does this work? Let's try it out with our string<\/p><pre class=\"codeinput\">fancyString = FancyString(openingLine);\r\n[fancyString.PlainValue]'\r\n<\/pre><pre class=\"codeoutput\">\r\nans = \r\n\r\n  12&times;1 string array\r\n\r\n    \"It\"\r\n    \"was\"\r\n    \"the\"\r\n    \"best\"\r\n    \"of\"\r\n    \"blogs\"\r\n    \"it\"\r\n    \"was\"\r\n    \"the\"\r\n    \"worst\"\r\n    \"of\"\r\n    \"blogs\"\r\n\r\n<\/pre><p>Alright, so far so good. Now we can start adding our fanciness! What I'd like to do is add the ability to decorate each string element with a decorator, and introduce the notion of a decorated value of the string. It is here I'd like to add the hyperlinks. Then, when I want to send the output to the Command Window I send the decorated value, but when I want to send the output to a text file I print the plain text. First's let's add the notion of a <i>default decorator<\/i>, which does nothing when decorated:<\/p><pre class=\"language-matlab\">\r\n<span class=\"keyword\">classdef<\/span> DefaultDecorator\r\n    \r\n    <span class=\"keyword\">methods<\/span>\r\n        <span class=\"keyword\">function<\/span> decoratedString = decorate(~, undecoratedString)\r\n            decoratedString =  undecoratedString;\r\n        <span class=\"keyword\">end<\/span>\r\n    <span class=\"keyword\">end<\/span>       \r\n\r\n<span class=\"keyword\">end<\/span>\r\n\r\n<\/pre><p>Now let's add a hyperlink decorator which decorates the string with a hyperlink tag. In this case we need the link target, but the link text should be taken as the value of the underlying string. I love how we can simply add these strings together to do this decoration!<\/p><pre class=\"language-matlab\">\r\n<span class=\"keyword\">classdef<\/span> HyperlinkDecorator\r\n   \r\n    <span class=\"keyword\">properties<\/span>\r\n        LinkTarget\r\n    <span class=\"keyword\">end<\/span>\r\n    \r\n    <span class=\"keyword\">methods<\/span>\r\n        <span class=\"keyword\">function<\/span> decorator = HyperlinkDecorator(linkTarget)\r\n            decorator.LinkTarget = linkTarget;\r\n        <span class=\"keyword\">end<\/span>\r\n        <span class=\"keyword\">function<\/span> decoratedString = decorate(decorator, undecoratedString)\r\n            decoratedString = <span class=\"string\">\"&lt;a href=\"\"\"<\/span> + decorator.LinkTarget + <span class=\"string\">\"\"\"&gt;\"<\/span> + undecoratedString + <span class=\"string\">\"&lt;\/a&gt;\"<\/span>;\r\n        <span class=\"keyword\">end<\/span>\r\n    <span class=\"keyword\">end<\/span>       \r\n    \r\n    \r\n<span class=\"keyword\">end<\/span>\r\n\r\n<\/pre><p>Alright, we are now set with our two decorators, let's use them with the FancyString:<\/p><pre class=\"language-matlab\">\r\n<span class=\"keyword\">classdef<\/span> FancyString \r\n    \r\n    <span class=\"keyword\">properties<\/span>\r\n        PlainValue\r\n        \r\n        <span class=\"comment\">% Each element uses the \"do nothing\" decorator by default<\/span>\r\n        Decorator = DefaultDecorator; \r\n    <span class=\"keyword\">end<\/span>\r\n    \r\n    <span class=\"keyword\">properties<\/span>(Dependent)\r\n        DecoratedValue\r\n    <span class=\"keyword\">end<\/span>\r\n    \r\n    \r\n    <span class=\"keyword\">methods<\/span>\r\n        <span class=\"keyword\">function<\/span> fancyString = FancyString(varargin)\r\n            <span class=\"keyword\">if<\/span> nargin &lt; 1\r\n                <span class=\"keyword\">return<\/span>\r\n            <span class=\"keyword\">end<\/span>\r\n                        \r\n            plainValue = string(varargin{:});\r\n            fancyString(numel(plainValue)) = FancyString;\r\n\r\n            plainValueCell = num2cell(plainValue);\r\n            [fancyString.PlainValue] = deal(plainValueCell{:});\r\n        <span class=\"keyword\">end<\/span>\r\n\r\n        <span class=\"keyword\">function<\/span> decorated = get.DecoratedValue(fancyString)\r\n            <span class=\"comment\">% The decorated value returns whatever the decorator adds to<\/span>\r\n            <span class=\"comment\">% the plain string.<\/span>\r\n            decorated = fancyString.Decorator.decorate(fancyString.PlainValue);\r\n        <span class=\"keyword\">end<\/span>\r\n            \r\n        <span class=\"keyword\">function<\/span> c = char(fancyString)\r\n            c = char(fancyString.PlainValue);\r\n        <span class=\"keyword\">end<\/span>\r\n        <span class=\"keyword\">function<\/span> c = cellstr(fancyString)\r\n            c = cellstr({fancyString.PlainValue});\r\n        <span class=\"keyword\">end<\/span>\r\n        <span class=\"keyword\">function<\/span> s = string(fancyString)\r\n            s = [fancyString.PlainValue];\r\n        <span class=\"keyword\">end<\/span>\r\n    <span class=\"keyword\">end<\/span>\r\n<span class=\"keyword\">end<\/span>\r\n\r\n<\/pre><p>We've added a <tt>Decorator<\/tt> property as well as a dependent <tt>DecoratedValue<\/tt> property. Since MATLAB is built for arrays this property is inherently array oriented. Each element of the FancyString has it's own Decorator. Also, when we access the <tt>DecoratedValue<\/tt> property on the array, each element of the array get's it own call to the <tt>get.DecoratedValue<\/tt> function to return the correct property. Let's see this beauty in action.<\/p><p>First we can create the <tt>FancyString<\/tt> again, but this time add a decorator to make it interesting. Let's add a hyperlink to the blog everywhere we see the word \"blogs\" in the plain value.<\/p><pre class=\"codeinput\">fancyString = FancyString(openingLine);\r\n[fancyString(contains([fancyString.PlainValue], <span class=\"string\">\"blogs\"<\/span>)).Decorator] = deal(HyperlinkDecorator(<span class=\"string\">\"https:\/\/blogs.mathworks.com\/developer\"<\/span>));\r\n<\/pre><p>Using MATLAB properties when writing the class, we've just leveraged indexing to add the desired hyperlink to those elements of the <tt>FancyString<\/tt> that contain the word blogs. Did it work?<\/p><pre class=\"codeinput\">decoratedValue = join([fancyString.DecoratedValue])\r\ndecorator = fancyString(end).Decorator\r\n<\/pre><pre class=\"codeoutput\">\r\ndecoratedValue = \r\n\r\n    \"It was the best of <a href=\"https:\/\/blogs.mathworks.com\/developer\">blogs<\/a> it was the worst of <a href=\"https:\/\/blogs.mathworks.com\/developer\">blogs<\/a>\"\r\n\r\n\r\ndecorator = \r\n\r\n  HyperlinkDecorator with properties:\r\n\r\n    LinkTarget: \"https:\/\/blogs.mathworks.com\/developer\"\r\n\r\n<\/pre><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/developer\/files\/y2017FancyStringScreenshot.png\" alt=\"\"> <\/p><p>Note the real underlying string is still available, unchanged and ready to display as well<\/p><pre class=\"codeinput\">plainValue = join([fancyString.PlainValue])\r\n<\/pre><pre class=\"codeoutput\">\r\nplainValue = \r\n\r\n    \"It was the best of blogs it was the worst of blogs\"\r\n\r\n<\/pre><p>Are you leveraging MATLAB properties effectively in your MATLAB objects? Are you leveraging the inherent benefits of their arrayness? Do tell!<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_1c129bd3dff74342be772bea139864f2() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='1c129bd3dff74342be772bea139864f2 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 1c129bd3dff74342be772bea139864f2';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2017 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_1c129bd3dff74342be772bea139864f2()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2017a<br><\/p><\/div><!--\r\n1c129bd3dff74342be772bea139864f2 ##### SOURCE BEGIN #####\r\n%% Properties of properties\r\n% \r\n% A while ago we discussed a good reason to appreciate and use MATLAB's\r\n% model for properties. It had to do with\r\n% <https:\/\/blogs.mathworks.com\/developer\/2016\/08\/08\/han-solo-encapsulation\/\r\n% encapsulation if you remember>, and we discussed how MATLAB's properties\r\n% are inherently encapsulated, preventing the need to write a bunch of\r\n% boiler plate setters and getters. \r\n%\r\n% There's more goodness where that came from! Another reason to use\r\n% MATLAB's properties is to benefit directly from the fact that everything\r\n% in MATLAB is considered an array that can be sliced and diced however you\r\n% please.\r\n%\r\n% The beauty if MATLAB is in its simplicity of handling arrays. Rather than\r\n% requiring some external container like a |List| or a |Vector| of\r\n% something, everything is inherently already a list and a vector! With\r\n% this you can be free to vectorize to your heart's content, as well as\r\n% index into your array using the powerful MATLAB syntax we all know and\r\n% love.\r\n%\r\n% This gets us back to object properties. If you use properties instead of\r\n% setter and getter methods you benefit from all this indexing sugar for\r\n% free. \r\n%\r\n%% \r\n%\r\n% *Pulling on strings*\r\n%\r\n% Case in point: Let's say we love the new string class in MATLAB. After\r\n% all what's not to love? The new MATLAB strings are rational and array\r\n% oriented, opening us to fast and powerful manipulation techniques. A\r\n% quick point is that we can indeed leverage this \"arrayness\" directly in\r\n% the string class itself without writing our own object code. For example,\r\n% if you wanted to quickly do some manipulation on a given string in order\r\n% to get your priorities straight you can do so quite easily. Let's start\r\n% by creating a string scalar:\r\n\r\nopeningLine = \"It was the best of times it was the worst of times\"\r\n\r\n%%\r\n% This is great, we can operate on this as a single string rather than the\r\n% old character vector approach where each character is an element of the\r\n% array. However, right now, I'd like to operate on this as an array of\r\n% words, so let's split it up:\r\n\r\nopeningLine = split(openingLine)\r\n\r\n%%\r\n% Now that this is an array of words, we can easily have it conform to our\r\n% nefarious demands! We can replace \"times\" with \"blogs\" simply using\r\n% the power of indexing:\r\n\r\nopeningLine(contains(openingLine, \"times\")) = \"blogs\"\r\n\r\n%% \r\n% Note for MATLAB strings the |replace| function is actually easier than\r\n% this, but I wanted to show the broader context of array indexing. In\r\n% practice with strings we should use |replace|.\r\nopeningLine = \"It was the best of times it was the worst of times\"\r\nopeningLine = split(openingLine)\r\nopeningLine = replace(openingLine, \"times\", \"blogs\")\r\n\r\n%%\r\n%\r\n% *Make it your own*\r\n% \r\n% Alright, vectorization and indexing are powerful in the MATLAB\r\n% environment, but how do we leverage this in our own objects? The answer\r\n% is to use properties! Let's say we want to create our own _fancy_ string\r\n% class which adds to the capabilities of the string class we see in\r\n% MATLAB. Perhaps we want to keep the underlying text content of the string\r\n% pure an unchanged, but we want to add specific meta-data to individual\r\n% elements. For example, in environments that support hyperlinks (like the\r\n% MATLAB command window) I may want to add a hyperlink, but I don't want\r\n% this hyperlink to modify the real text content. Maybe I'd like to print\r\n% that to a text file and the hyperlink would get in the way. Let's\r\n% look at how we can do this, and benefit from the indexing behavior of\r\n% MATLAB's properties.\r\n%\r\n% First, we should create our |FancyString| class. Note we leverage\r\n% composition over inheritance\r\n% (<https:\/\/blogs.mathworks.com\/developer\/2015\/05\/01\/composition-over-inheritance\/\r\n% remember>?) and we hold on to the \"plain\" string. However, the\r\n% constructor passes all values through to the string constructor so\r\n% that it feels like a normal string. Note we can also add converter methods\r\n% for char, string, and cellstr so that we can combine these values\r\n% together effortlessly.\r\n%\r\n% <include>FancyString_v1.m<\/include>\r\n% \r\n% Does this work? Let's try it out with our string\r\nfancyString = FancyString(openingLine);\r\n[fancyString.PlainValue]'\r\n\r\n%%\r\n% Alright, so far so good. Now we can start adding our fanciness! What I'd\r\n% like to do is add the ability to decorate each string element with a\r\n% decorator, and introduce the notion of a decorated value of the string.\r\n% It is here I'd like to add the hyperlinks. Then, when I want to send the\r\n% output to the Command Window I send the decorated value, but when I want\r\n% to send the output to a text file I print the plain text. First's let's\r\n% add the notion of a _default decorator_, which does nothing when\r\n% decorated:\r\n%\r\n% <include>DefaultDecorator.m<\/include>\r\n%\r\n% Now let's add a hyperlink decorator which decorates the string with a\r\n% hyperlink tag. In this case we need the link target, but the link text\r\n% should be taken as the value of the underlying string. I love how we can\r\n% simply add these strings together to do this decoration!\r\n%\r\n% <include>HyperlinkDecorator.m<\/include>\r\n%\r\n% Alright, we are now set with our two decorators, let's use them with the\r\n% FancyString:\r\n%\r\n% <include>FancyString.m<\/include>\r\n%\r\n% We've added a |Decorator| property as well as a dependent\r\n% |DecoratedValue| property. Since MATLAB is built for arrays this property\r\n% is inherently array oriented. Each element of the FancyString has it's\r\n% own Decorator. Also, when we access the |DecoratedValue| property on the\r\n% array, each element of the array get's it own call to the\r\n% |get.DecoratedValue| function to return the correct property. Let's see\r\n% this beauty in action.\r\n%\r\n% First we can create the |FancyString| again, but this time add a\r\n% decorator to make it interesting. Let's add a hyperlink to the blog\r\n% everywhere we see the word \"blogs\" in the plain value.\r\n\r\nfancyString = FancyString(openingLine);\r\n[fancyString(contains([fancyString.PlainValue], \"blogs\")).Decorator] = deal(HyperlinkDecorator(\"https:\/\/blogs.mathworks.com\/developer\"));\r\n\r\n%%\r\n% Using MATLAB properties when writing the class, we've just leveraged\r\n% indexing to add the desired hyperlink to those elements of the\r\n% |FancyString| that contain the word blogs. Did it work?\r\ndecoratedValue = join([fancyString.DecoratedValue])\r\ndecorator = fancyString(end).Decorator\r\n\r\n%%\r\n%\r\n% <<y2017FancyStringScreenshot.png>>\r\n% \r\n% Note the real underlying string is still available, unchanged and ready\r\n% to display as well\r\nplainValue = join([fancyString.PlainValue])\r\n\r\n%%\r\n% Are you leveraging MATLAB properties effectively in your MATLAB objects?\r\n% Are you leveraging the inherent benefits of their arrayness? Do tell!\r\n\r\n##### SOURCE END ##### 1c129bd3dff74342be772bea139864f2\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/developer\/files\/y2017FancyStringScreenshot.png\" onError=\"this.style.display ='none';\" \/><\/div><!--introduction--><p>A while ago we discussed a good reason to appreciate and use MATLAB's model for properties. It had to do with <a href=\"https:\/\/blogs.mathworks.com\/developer\/2016\/08\/08\/han-solo-encapsulation\/\">encapsulation if you remember<\/a>, and we discussed how MATLAB's properties are inherently encapsulated, preventing the need to write a bunch of boiler plate setters and getters.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/developer\/2017\/04\/04\/properties-of-properties\/\">read more >><\/a><\/p>","protected":false},"author":90,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12,16,5],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/posts\/916"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/users\/90"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/comments?post=916"}],"version-history":[{"count":6,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/posts\/916\/revisions"}],"predecessor-version":[{"id":923,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/posts\/916\/revisions\/923"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/media?parent=916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/categories?post=916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/tags?post=916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}