{"id":7950,"date":"2016-10-21T09:00:33","date_gmt":"2016-10-21T13:00:33","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=7950"},"modified":"2016-10-18T09:18:44","modified_gmt":"2016-10-18T13:18:44","slug":"write-your-figures-to-excel","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2016\/10\/21\/write-your-figures-to-excel\/","title":{"rendered":"Write Your Figures to Excel"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\n   <introduction><\/p>\n<p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495\">Sean<\/a>&#8216;s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24424-xlswritefig\"><tt>xlswritefig<\/tt><\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869375\">Michelle Hirsch<\/a>.\n      <\/p>\n<p>   <\/introduction><\/p>\n<p>One of my colleagues approached me earlier this week and asked me to help her with an Excel report that I&#8217;d created a few<br \/>\n      months back.  When I first created this report, I just did it manually thinking it was a one time thing.  But, she wanted<br \/>\n      to create the same report many times.\n   <\/p>\n<p>My first question was: Does it have to be Excel or could I create Word or PowerPoint reports instead?  I didn&#8217;t know her end<br \/>\n      goal for the reports so figured it would be easier to automate reporting for those formats with <a href=\"https:\/\/www.mathworks.com\/products\/ML_reportgenerator\/\">MATLAB Report Generator<\/a>.  However, Excel was a requirement.\n   <\/p>\n<p>There were some data from a table and a few figures that needed to be written and fortunately no other formatting, e.g.<br \/>\n      conditional formatting.  The table was easy; a simple call to <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/writetable.html\"><tt>writetable<\/tt><\/a> sufficed.\n   <\/p>\n<p>But how to export a figure?  I didn&#8217;t want to deal with the Excel API myself, so I went to the File Exchange and searched<br \/>\n      for &#8220;figure to Excel&#8221;.  There were a few results, but Michelle&#8217;s <tt>xlswritefig<\/tt> seemed to have the most options and it worked on first try!\n   <\/p>\n<p>Here&#8217;s some random data for next 24 hours.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">nexthour = dateshift(datetime(<span style=\"color: #A020F0\">'now'<\/span>), <span style=\"color: #A020F0\">'end'<\/span>, <span style=\"color: #A020F0\">'hour'<\/span>);\r\nTime = (nexthour:hours(1):(datetime(<span style=\"color: #A020F0\">'now'<\/span>)+hours(24))).';\r\nData = cumsum(rand(size(Time)) - 0.2);<\/pre>\n<p>Plot the data. <b>Note<\/b> that the convenience functions <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/xticks.html\"><tt>xticks<\/tt><\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/xtickangle.html\"><tt>xtickangle<\/tt><\/a> along with many others are new in <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/release-notes.html?rntext=&amp;startrelease=R2016b&amp;endrelease=R2016b\">R2016b<\/a>!\n   <\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">plot(Time, Data)\r\nxticks(Time(1:3:end))\r\nxtickangle(-45)\r\nxlabel(<span style=\"color: #A020F0\">'Next 24 Hours'<\/span>)\r\nylabel(<span style=\"color: #A020F0\">'Randomness'<\/span>)\r\ntitle(<span style=\"color: #A020F0\">'A Plot'<\/span>)<\/pre>\n<p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainxlswritefig\/mainxlswritefig_01.png\"> <\/p>\n<p>Write to Excel.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">writetable(table(Time, Data), <span style=\"color: #A020F0\">'Results.xlsx'<\/span>, <span style=\"color: #A020F0\">'WriteVariableNames'<\/span>, true)\r\nxlswritefig(gcf, <span style=\"color: #A020F0\">'Results.xlsx'<\/span>, <span style=\"color: #A020F0\">'Sheet1'<\/span>, <span style=\"color: #A020F0\">'D2'<\/span>)<\/pre>\n<p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainxlswritefig\/xlsresults.png\"> <\/p>\n<h3>Comments<a name=\"6\"><\/a><\/h3>\n<p>Does creating formatted reports from MATLAB interest you?  If so, what would you like to do?<\/p>\n<p>Give it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=7950#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24424-xlswritefig#comments\">comment<\/a> for Michelle.\n   <\/p>\n<p><script language=\"JavaScript\">\n<!--\n\n    function grabCode_f7d3a760f0b94f39adf2f842baae01db() {\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='f7d3a760f0b94f39adf2f842baae01db ' + '##### ' + 'SOURCE BEGIN' + ' #####';\n        t2='##### ' + 'SOURCE END' + ' #####' + ' f7d3a760f0b94f39adf2f842baae01db';\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        author = 'Sean de Wolski';\n        copyright = 'Copyright 2016 The MathWorks, Inc.';\n\n        w = window.open();\n        d = w.document;\n        d.write('\n\n<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\n\n\\n');\n      \n      d.title = title + ' (MATLAB code)';\n      d.close();\n      }   \n      \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_f7d3a760f0b94f39adf2f842baae01db()\"><span style=\"font-size: x-small;        font-style: italic;\">Get<br \/>\n            the MATLAB code<br \/>\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><\/p>\n<p>      Published with MATLAB&reg; R2016b<\/p>\n<\/div>\n<p><!--\nf7d3a760f0b94f39adf2f842baae01db ##### SOURCE BEGIN #####\n%% xlswritefig\n%\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495 Sean>'s pick this week is\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24424-xlswritefig |xlswritefig|> by\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869375 Michelle Hirsch>.\n% \n\n%% \n% One of my colleagues approached me earlier this week and asked me to help\n% her with an Excel report that I'd created a few months back.  When I\n% first created this report, I just did it manually thinking it was a one\n% time thing.  But, she wanted to create the same report many times.\n%\n% My first question was: Does it have to be Excel or could I create Word or\n% PowerPoint reports instead?  I didn't know her end goal for the reports\n% so figured it would be easier to automate reporting for those formats\n% with <https:\/\/www.mathworks.com\/products\/ML_reportgenerator\/ MATLAB\n% Report Generator>.  However, Excel was a requirement.\n%\n% There were some data from a table and a few figures that needed to be\n% offloaded and fortunately no other formatting, e.g. conditional\n% formatting.  The table is easy; a simple call to\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/writetable.html |writetable|>\n% would suffice.\n% \n% But how to export a figure?  I didn't want to deal with the Excel API\n% myself, so I went to the File Exchange and searched for \"figure to\n% Excel\".  There were a few results, but Michelle's |xlswritefig| seemed to\n% have the most options and it worked on first try!\n\n%%\n% Random data for next 24 hours.\nnexthour = dateshift(datetime('now'), 'end', 'hour');\nTime = (nexthour:hours(1):(datetime('now')+hours(24))).';\nData = cumsum(rand(size(Time)) - 0.2);\n\n%%\n% Plot the data. *Note* that the convenience functions\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/xticks.html |xticks|> and\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/xtickangle.html |xtickangle|>\n% along with many others are new in\n% <https:\/\/www.mathworks.com\/help\/matlab\/release-notes.html?rntext=&startrelease=R2016b&endrelease=R2016b\n% R2016b>!\n\nplot(Time, Data)\nxticks(Time(1:3:end))\nxtickangle(-45)\nxlabel('Next 24 Hours')\nylabel('Randomness')\ntitle('A Plot')\n\n%%\n% Write to Excel\n\nwritetable(table(Time, Data), 'Results.xlsx', 'WriteVariableNames', true)\nxlswritefig(gcf, 'Results.xlsx', 'Sheet1', 'D2')\n\n%%\n%\n% <<xlsresults.png>>\n\n%% Comments\n% \n% Does creating formatted reports from MATLAB interest you?  If so, what\n% would you like to do?\n%\n% Give it a try and let us know what you think\n% <https:\/\/blogs.mathworks.com\/pick\/?p=7950#respond here> or leave a\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24424-xlswritefig#comments\n% comment> for Michelle.\n##### SOURCE END ##### f7d3a760f0b94f39adf2f842baae01db\n--><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainxlswritefig\/mainxlswritefig_01.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>Sean&#8216;s pick this week is xlswritefig by Michelle Hirsch.<\/p>\n<p>One of my colleagues approached me earlier this week and asked me to help her with an Excel report that I&#8217;d&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2016\/10\/21\/write-your-figures-to-excel\/\">read more >><\/a><\/p>\n","protected":false},"author":87,"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\/7950"}],"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\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=7950"}],"version-history":[{"count":7,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/7950\/revisions"}],"predecessor-version":[{"id":7958,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/7950\/revisions\/7958"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=7950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=7950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=7950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}