{"id":8017,"date":"2016-11-11T09:00:46","date_gmt":"2016-11-11T14:00:46","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=8017"},"modified":"2016-11-09T01:05:05","modified_gmt":"2016-11-09T06:05:05","slug":"r2016b-features-markerindices-jsonencode-jsondecode","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2016\/11\/11\/r2016b-features-markerindices-jsonencode-jsondecode\/","title":{"rendered":"R2016b Features: MarkerIndices, jsonencode, jsondecode"},"content":{"rendered":"<div class=\"content\">\n<p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>&#8216;s picks this week are two of the many new features of R2016b: <a title=\"https:\/\/www.mathworks.com\/help\/releases\/R2016b\/matlab\/ref\/chartline-properties.html#property_MarkerIndices (link no longer works)\"><tt>MarkerIndices<\/tt> property for specifying marker locations<\/a> and <a title=\"https:\/\/www.mathworks.com\/help\/releases\/R2016b\/matlab\/ref\/jsonencode.html (link no longer works)\"><tt>jsonencode<\/tt><\/a>\/ <a title=\"https:\/\/www.mathworks.com\/help\/releases\/R2016b\/matlab\/ref\/jsondecode.html (link no longer works)\"><tt>jsondecode<\/tt><\/a>.<\/p>\n<p>In the past, I&#8217;ve highlighted a couple of new product features which were related to previous Picks: <a href=\"https:\/\/blogs.mathworks.com\/pick\/2016\/03\/25\/interactive-legend-in-r2016a\">&#8220;Interactive Legend in R 2016a&#8221;<\/a> and <a href=\"https:\/\/blogs.mathworks.com\/pick\/2015\/11\/06\/does-your-origin-go-through-zero\">&#8220;Does your origin go through zero?&#8221;<\/a>. This week&#8217;s post is of the same nature.<\/p>\n<p><b><tt>MarkerIndices<\/tt><\/b><\/p>\n<p>Back in 2013, I posted this blog on <a href=\"https:\/\/blogs.mathworks.com\/pick\/2013\/08\/16\/line-plots-with-fewer-markers\/\">creating line plots with fewer markers<\/a>. This is especially useful when you have a dense (high-sampled) signal that you want to plot with markers. In R2016b, you can use the new <tt>MarkerIndices<\/tt> property to specify which points to add a marker to.<\/p>\n<p>For example, if you want 10 evenly-spaced markers, you can do the following.<\/p>\n<pre class=\"codeinput\">t  = 0:0.005:pi;\nplot(t, sin(3*t).*cos(t\/2), <span class=\"string\">'p-'<\/span>, <span class=\"string\">'MarkerIndices'<\/span>, round(linspace(1,length(t),10)))\n<\/pre>\n<p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_r2016b_new_features\/potw_r2016b_new_features_01.png\" alt=\"\"> <\/p>\n<p><b><tt>jsonencode<\/tt> \/ <tt>jsondecode<\/tt><\/b><\/p>\n<p>In April 2015, I posted another blog on <a href=\"https:\/\/blogs.mathworks.com\/pick\/2015\/04\/24\/parsing-json-files\">parsing JSON files<\/a>. In R2016b, we now have <tt>jsonencode<\/tt> and <tt>jsondecode<\/tt>. I&#8217;ll use the same example I used in the 2015 blog post. Let&#8217;s say we have <tt>example.json<\/tt> the has the following content.<\/p>\n<pre>{\n    \"firstName\": \"John\",\n    \"lastName\": \"Smith\",\n    \"age\": 25,\n    \"address\":\n    {\n        \"streetAddress\": \"3 Apple Hill Dr\",\n        \"city\": \"Natick\",\n        \"state\": \"MA\",\n        \"postalCode\": \"01760\"\n    },\n    \"phoneNumber\":\n    [\n        {\n          \"type\": \"home\",\n          \"number\": \"123 456 7890\"\n        },\n        {\n          \"type\": \"cell\",\n          \"number\": \"098 765 4321\"\n        }\n    ]\n}<\/pre>\n<p><tt>jsondecode<\/tt> works on character arrays, so we will first read in the text data.<\/p>\n<pre class=\"codeinput\">str = fileread(<span class=\"string\">'example.json'<\/span>);\n<\/pre>\n<p>Then, simply call <tt>jsondecode<\/tt>.<\/p>\n<pre class=\"codeinput\">data = jsondecode(str)\n<\/pre>\n<pre class=\"codeoutput\">data = \n  struct with fields:\n\n      firstName: 'John'\n       lastName: 'Smith'\n            age: 25\n        address: [1&times;1 struct]\n    phoneNumber: [2&times;1 struct]\n<\/pre>\n<p>The output is a structure.<\/p>\n<pre class=\"codeinput\">disp(data.address)\n<\/pre>\n<pre class=\"codeoutput\">    streetAddress: '3 Apple Hill Dr'\n             city: 'Natick'\n            state: 'MA'\n       postalCode: '01760'\n<\/pre>\n<pre class=\"codeinput\">disp(data.phoneNumber(1))\n<\/pre>\n<pre class=\"codeoutput\">      type: 'home'\n    number: '123 456 7890'\n<\/pre>\n<p><b>Comments<\/b><\/p>\n<p>Give these new features a try. If you&#8217;re using pre-R2016b MATLAB, be sure to check out the blog posts I mentioned above. Let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=8017#respond\">here<\/a>.<\/p>\n<p><script language=\"JavaScript\"> <!-- \n    function grabCode_f00601e52342486ba86f4f65d024613e() {\n        \/\/ Remember the title so we can use it in the new page\n        title = document.title;\n\n        \/\/ Break up these strings so that their presence\n        \/\/ in the Javascript doesn't mess up the search for\n        \/\/ the MATLAB code.\n        t1='f00601e52342486ba86f4f65d024613e ' + '##### ' + 'SOURCE BEGIN' + ' #####';\n        t2='##### ' + 'SOURCE END' + ' #####' + ' f00601e52342486ba86f4f65d024613e';\n    \n        b=document.getElementsByTagName('body')[0];\n        i1=b.innerHTML.indexOf(t1)+t1.length;\n        i2=b.innerHTML.indexOf(t2);\n \n        code_string = b.innerHTML.substring(i1, i2);\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\n\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \n        \/\/ in the XML parser.\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\n        \/\/ doesn't go ahead and substitute the less-than character. \n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\n\n        copyright = 'Copyright 2016 The MathWorks, Inc.';\n\n        w = window.open();\n        d = w.document;\n        d.write('\n\n<pre>\\n');\n        d.write(code_string);\n\n        \/\/ Add copyright line at the bottom if specified.\n        if (copyright.length > 0) {\n            d.writeln('');\n            d.writeln('%%');\n            if (copyright.length > 0) {\n                d.writeln('% _' + copyright + '_');\n            }\n        }\n\n        d.write('<\/pre>\n\n\\n');\n\n        d.title = title + ' (MATLAB code)';\n        d.close();\n    }   \n     --> <\/script><\/p>\n<p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><a href=\"javascript:grabCode_f00601e52342486ba86f4f65d024613e()\"><span style=\"font-size: x-small;        font-style: italic;\">Get<br \/>\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><\/p>\n<p class=\"footer\">\n      Published with MATLAB&reg; R2016b<\/p>\n<\/div>\n<p><!--\nf00601e52342486ba86f4f65d024613e ##### SOURCE BEGIN #####\n%%\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\n% Jiro>'s picks this week are two of the many new features of R2016b:\n% <https:\/\/www.mathworks.com\/help\/releases\/R2016b\/matlab\/ref\/chartline-properties.html#property_MarkerIndices\n% |MarkerIndices| property for specifying marker locations> and\n% <https:\/\/www.mathworks.com\/help\/releases\/R2016b\/matlab\/ref\/jsonencode.html\n% |jsonencode|>\/\n% <https:\/\/www.mathworks.com\/help\/releases\/R2016b\/matlab\/ref\/jsondecode.html\n% |jsondecode|>.\n%\n% In the past, I've highlighted a couple of new product features which were\n% related to previous Picks:\n% <https:\/\/blogs.mathworks.com\/pick\/2016\/03\/25\/interactive-legend-in-r2016a\n% \"Interactive Legend in R 2016a\"> and\n% <https:\/\/blogs.mathworks.com\/pick\/2015\/11\/06\/does-your-origin-go-through-zero\n% \"Does your origin go through zero?\">. This week's post is of the same\n% nature.\n%\n% *|MarkerIndices|*\n%\n% Back in 2013, I posted this blog on\n% <https:\/\/blogs.mathworks.com\/pick\/2013\/08\/16\/line-plots-with-fewer-markers\/\n% creating line plots with fewer markers>. This is especially useful when\n% you have a dense (high-sampled) signal that you want to plot with\n% markers. In R2016b, you can use the new |MarkerIndices| property to\n% specify which points to add a marker to.\n%\n% For example, if you want 10 evenly-spaced markers, you can do the\n% following.\n\nt  = 0:0.005:pi;\nplot(t, sin(3*t).*cos(t\/2), 'p-', 'MarkerIndices', round(linspace(1,length(t),10)))\n\n%%\n% *|jsonencode| \/ |jsondecode|*\n%\n% In April 2015, I posted another blog on\n% <https:\/\/blogs.mathworks.com\/pick\/2015\/04\/24\/parsing-json-files parsing\n% JSON files>. In R2016b, we now have |jsonencode| and |jsondecode|. I'll\n% use the same example I used in the 2015 blog post. Let's say we have\n% |example.json| the has the following content.\n%\n%  {\n%      \"firstName\": \"John\",\n%      \"lastName\": \"Smith\",\n%      \"age\": 25,\n%      \"address\":\n%      {\n%          \"streetAddress\": \"3 Apple Hill Dr\",\n%          \"city\": \"Natick\",\n%          \"state\": \"MA\",\n%          \"postalCode\": \"01760\"\n%      },\n%      \"phoneNumber\":\n%      [\n%          {\n%            \"type\": \"home\",\n%            \"number\": \"123 456 7890\"\n%          },\n%          {\n%            \"type\": \"cell\",\n%            \"number\": \"098 765 4321\"\n%          }\n%      ]\n%  }\n%\n% |jsondecode| works on character arrays, so we will first read in the text\n% data.\n\nstr = fileread('example.json');\n\n%%\n% Then, simply call |jsondecode|.\n\ndata = jsondecode(str)\n\n%%\n% The output is a structure.\n\ndisp(data.address)\n\n%%\n\ndisp(data.phoneNumber(1))\n\n%%\n% *Comments*\n%\n% Give these new features a try. If you're using pre-R2016b MATLAB, be sure\n% to check out the blog posts I mentioned above. Let us know what you think\n% <https:\/\/blogs.mathworks.com\/pick\/?p=8017#respond here>.\n\n##### SOURCE END ##### f00601e52342486ba86f4f65d024613e\n--><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_r2016b_new_features\/potw_r2016b_new_features_01.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>\nJiro&#8216;s picks this week are two of the many new features of R2016b: MarkerIndices property for specifying marker locations and jsonencode\/ jsondecode.<br \/>\nIn the past, I&#8217;ve highlighted a&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2016\/11\/11\/r2016b-features-markerindices-jsonencode-jsondecode\/\">read more >><\/a><\/p>\n","protected":false},"author":35,"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\/8017"}],"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\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=8017"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8017\/revisions"}],"predecessor-version":[{"id":8020,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8017\/revisions\/8020"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=8017"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=8017"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=8017"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}