{"id":5961,"date":"2015-05-01T09:00:39","date_gmt":"2015-05-01T13:00:39","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=5961"},"modified":"2015-05-01T15:21:09","modified_gmt":"2015-05-01T19:21:09","slug":"remove-default-excel-sheets","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2015\/05\/01\/remove-default-excel-sheets\/","title":{"rendered":"Remove Default Excel Sheets"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495\">Sean<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/50371-removesheet123-remove-default-excel-sheets\">RemoveSheet123<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/5172536\">Noam Greenboim<\/a>.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Working with MATLAB And Excel<a name=\"1\"><\/a><\/h3>\r\n   <p>Recently, I've been doing data analysis for colleagues who are not MATLAB users.  However, most of them are familiar with\r\n      Excel.  Thus, I do the heavy lifting in MATLAB, such as aggregating millions of records from a database, or <a href=\"\">complex set<\/a> analysis, and export the results to Excel so they can see it, manipulate it, or pivot it out in Excel.\r\n   <\/p>\r\n   <p>To facilitate this, my mode of operation has been to take the data and stick it into a <a href=\"\"><tt>table<\/tt><\/a> and then use <a href=\"\"><tt>writetable<\/tt><\/a> to export to Excel.  In general, <tt>writetable<\/tt> \"does the right thing\".  If you have heterogenous data, it figures out how to write it, the variable names already exist\r\n      so those are used as column headers, etc.  I find this to be a simple workflow for most cases where I don't usually need the\r\n      additional control <tt>xlswrite<\/tt> offers.\r\n   <\/p>\r\n   <p>There are two things about this approach that have to be taken care of though.  First, if you write to a named sheet, which\r\n      I always do, you'll get a warning for adding a new sheet to the Excel file.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">writetable(table(rand(5)),<span style=\"color: #A020F0\">'SimpleExcelFile.xlsx'<\/span>,<span style=\"color: #A020F0\">'Sheet'<\/span>,<span style=\"color: #A020F0\">'Rand5'<\/span>)<\/pre><pre style=\"font-style:oblique\">Warning: Added specified\r\nworksheet. \r\n<\/pre><p>Fortunately, this can be turned off pretty easily.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">warning(<span style=\"color: #A020F0\">'off'<\/span>,<span style=\"color: #A020F0\">'MATLAB:xlswrite:AddSheet'<\/span>);<\/pre><p>I actually turn off this warning in my <a href=\"\"><tt>startup.m<\/tt><\/a> file so I never need to see this message except when blogging about it!\r\n   <\/p>\r\n   <p>The other thing that happens with this approach is that the default Excel sheets are not used, remaining empty and taking\r\n      up space or time when clicking on them to see what they contain.  And this is where Noam's file comes in: It removes those\r\n      default sheets, plain and simple.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% Download Some Data from Yahoo! (Requires Datafeed Toolbox)<\/span>\r\ny = yahoo;\r\n\r\n<span style=\"color: #228B22\">% Fetch<\/span>\r\nClosePrice = fetch(y,<span style=\"color: #A020F0\">'SAM'<\/span>,<span style=\"color: #A020F0\">'Close'<\/span>,<span style=\"color: #A020F0\">'04\/01\/15'<\/span>,<span style=\"color: #A020F0\">'04\/30\/15'<\/span>);\r\n\r\n<span style=\"color: #228B22\">% Convert to table and datetime with descriptive variable names<\/span>\r\nTClosePrice = table(datetime(ClosePrice(:,1),<span style=\"color: #A020F0\">'ConvertFrom'<\/span>,<span style=\"color: #A020F0\">'datenum'<\/span>),ClosePrice(:,2));\r\nTClosePrice.Properties.VariableNames = {<span style=\"color: #A020F0\">'Date'<\/span>,<span style=\"color: #A020F0\">'SamAdamsClosingPrice'<\/span>};\r\n\r\n<span style=\"color: #228B22\">% Write it<\/span>\r\nwritetable(TClosePrice,<span style=\"color: #A020F0\">'StockData.xlsx'<\/span>,<span style=\"color: #A020F0\">'Sheet'<\/span>,<span style=\"color: #A020F0\">'SamAdams'<\/span>)<\/pre><p>Before:<\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainrmsheets\/before.png\"> <\/p>\r\n   <p><b>Note:<\/b> This only works on Windows because it relies on the Excel API.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% Remove sheets<\/span>\r\nRemoveSheet123(<span style=\"color: #A020F0\">'StockData.xlsx'<\/span>);<\/pre><pre style=\"font-style:oblique\">\r\nsheet #1 - deleted.<\/pre><p>After:<\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainrmsheets\/after.png\"> <\/p>\r\n   <p>Thanks Noam for saving me the effort of researching the API for Excel.<\/p>\r\n   <p>I did make one small change which was to comment out the <b>catch<\/b> block after the delete (line 59) so if it it fails, it does so silently.  The reason for this is that I have a different\r\n      Excel template that gives me just one sheet by default.  I never have to delete all three, at least not on this machine. \r\n      This change takes care of the fact that when the error is thrown, the document is not saved or exited.  I'd recommend using\r\n      an <a href=\"\"><tt>onCleanup<\/tt><\/a> object to prevent this state from being possible.\r\n   <\/p>\r\n   <h3>Comments<a name=\"7\"><\/a><\/h3>\r\n   <p>How do you integrate MATLAB and Excel?  Any tips, tricks, or challenges, you'd like to share for some MathWorks swag?<\/p>\r\n   <p>Give it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=5961#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/50371-removesheet123-remove-default-excel-sheets#comments\">comment<\/a> for Noam.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_097516a6be664976ab81408e93676d19() {\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='097516a6be664976ab81408e93676d19 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 097516a6be664976ab81408e93676d19';\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        author = 'Sean de Wolski';\r\n        copyright = 'Copyright 2015 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 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');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \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_097516a6be664976ab81408e93676d19()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2015a<br><\/p>\r\n<\/div>\r\n<!--\r\n097516a6be664976ab81408e93676d19 ##### SOURCE BEGIN #####\r\n%% RemoveSheet123 - Remove Default Excel Sheets\r\n%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495 Sean>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/50371-removesheet123-remove-default-excel-sheets RemoveSheet123> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/5172536 Noam Greenboim>.\r\n% \r\n\r\n%% Working with MATLAB And Excel\r\n%\r\n% Recently, I've been doing data analysis for colleagues who are not MATLAB\r\n% users.  However, most of them are familiar with Excel.  Thus, I do the\r\n% heavy lifting in MATLAB, such as aggregating millions of records from a\r\n% database, or\r\n% <\r\n% complex set> analysis, and export the results to Excel so they can see\r\n% it, manipulate it, or pivot it out in Excel.\r\n%\r\n% To facilitate this, my mode of operation has been to take the data and\r\n% stick it into a\r\n% <\r\n% |table|> and then use\r\n% <\r\n% |writetable|> to export to Excel.  In general, |writetable| \"does the\r\n% right thing\".  If you have heterogenous data, it figures out how to write\r\n% it, the variable names already exist so those are used as column headers,\r\n% etc.  I find this to be a simple workflow for most cases where I don't\r\n% usually need the additional control |xlswrite| offers.\r\n%\r\n% There are two things about this approach that have to be taken care of\r\n% though.  First, if you write to a named sheet, which I always do, you'll\r\n% get a warning for adding a new sheet to the Excel file.\r\n\r\nwritetable(table(rand(5)),'SimpleExcelFile.xlsx','Sheet','Rand5')\r\n\r\n\r\n%%\r\n% Fortunately, this can be turned off pretty easily.  \r\n\r\nwarning('off','MATLAB:xlswrite:AddSheet');\r\n\r\n%%\r\n% I actually turn off this warning in my\r\n% <\r\n% |startup.m|> file so I never need to see this message except when\r\n% blogging about it!\r\n%\r\n% The other thing that happens with this approach is that the default Excel\r\n% sheets are not used, remaining empty and taking up space or time when\r\n% clicking on them to see what they contain.  And this is where Noam's file\r\n% comes in: It removes those default sheets, plain and simple.\r\n\r\n% Download Some Data from Yahoo!\r\ny = yahoo;\r\n\r\n% Fetch\r\nClosePrice = fetch(y,'SAM','Close','04\/01\/15','04\/30\/15');\r\n\r\n% Convert to table and datetime with descriptive variable names\r\nTClosePrice = table(datetime(ClosePrice(:,1),'ConvertFrom','datenum'),ClosePrice(:,2));\r\nTClosePrice.Properties.VariableNames = {'Date','SamAdamsClosingPrice'};\r\n\r\n% Write it\r\nwritetable(TClosePrice,'StockData.xlsx','Sheet','SamAdams')\r\n\r\n%% \r\n% Before:\r\n%\r\n% <<before.png>>\r\n%\r\n% *Note:* This only works on Windows because it relies on the Excel API.\r\n\r\n% Remove sheets\r\nRemoveSheet123('StockData.xlsx');\r\n\r\n%%\r\n% After:\r\n%\r\n% <<after.png>>\r\n\r\n\r\n%% \r\n% Thanks Noam for saving me the effort of researching the API for Excel.\r\n%\r\n% I did make one small change which was to comment out the *catch* block\r\n% after the delete (line 59) so if it it fails, it does so silently.  The\r\n% reason for this is that I have a different Excel template that gives me\r\n% just one sheet by default.  I never have to delete all three, at least\r\n% not on this machine.  This change takes care of the fact that when the\r\n% error is thrown, the document is not saved or exited.  I'd recommend\r\n% using an\r\n% <\r\n% |onCleanup|> object to prevent this state from being possible.\r\n\r\n\r\n%% Comments\r\n% \r\n% How do you integrate MATLAB and Excel?  Any tips, tricks, or challenges,\r\n% you'd like to share for some MathWorks swag?\r\n%\r\n% Give it a try and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=5961#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/50371-removesheet123-remove-default-excel-sheets#comments\r\n% comment> for Noam.\r\n%\r\n \r\n\r\n##### SOURCE END ##### 097516a6be664976ab81408e93676d19\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainrmsheets\/before.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n   \r\n      Sean's pick this week is RemoveSheet123 by Noam Greenboim.\r\n      \r\n   \r\n   Working with MATLAB And Excel\r\n   Recently, I've been doing data analysis for colleagues who are not MATLAB... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2015\/05\/01\/remove-default-excel-sheets\/\">read more >><\/a><\/p>","protected":false},"author":87,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,6],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5961"}],"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=5961"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5961\/revisions"}],"predecessor-version":[{"id":5963,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5961\/revisions\/5963"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=5961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=5961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=5961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}