{"id":2827,"date":"2017-01-05T12:20:16","date_gmt":"2017-01-05T17:20:16","guid":{"rendered":"https:\/\/blogs.mathworks.com\/videos\/?p=2827"},"modified":"2017-01-26T14:13:39","modified_gmt":"2017-01-26T19:13:39","slug":"creating-a-function-to-save-a-table-to-a-csv-if-its-too-big-for-excel","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/videos\/2017\/01\/05\/creating-a-function-to-save-a-table-to-a-csv-if-its-too-big-for-excel\/","title":{"rendered":"Creating a Function to Save a Table to a CSV File if it&#8217;s Too Big for Excel"},"content":{"rendered":"<p>I often save tables to Excel for sharing with others, but if the data is too large, I need to share to a CSV file instead. Here I build a function to do that. Features covered in this <a href=\"https:\/\/blogs.mathworks.com\/videos\/2015\/10\/29\/matlab-code-along-videos\/\">code-along<\/a> style video include:<\/p>\n<ul>\n<li><tt>writetable<\/tt><\/li>\n<li><tt>regexprep<\/tt><\/li>\n<\/ul>\n<p>Note, at @18:13 I think I&#8217;m looking at the Excel file and don&#8217;t understand the first sheet name, but I&#8217;m actually looking at the CSV file and the sheet name defaults to the filename. You can ignore my floundering for the remaining 5 min of the video if you like. <\/p>\n<p><div class=\"row\"><div class=\"col-xs-12 containing-block\"><div class=\"bc-outer-container add_margin_20\"><videoplayer><div class=\"video-js-container\"><video data-video-id=\"5271714808001\" data-video-category=\"blog\" data-autostart=\"false\" data-account=\"62009828001\" data-omniture-account=\"mathwgbl\" data-player=\"rJ9XCz2Sx\" data-embed=\"default\" id=\"mathworks-brightcove-player\" class=\"video-js\" controls><\/video><script src=\"\/\/players.brightcove.net\/62009828001\/rJ9XCz2Sx_default\/index.min.js\"><\/script><script>if (typeof(playerLoaded) === 'undefined') {var playerLoaded = false;}(function isVideojsDefined() {if (typeof(videojs) !== 'undefined') {videojs(\"mathworks-brightcove-player\").on('loadedmetadata', function() {playerLoaded = true;});} else {setTimeout(isVideojsDefined, 10);}})();<\/script><\/div><\/videoplayer><\/div><\/div><\/div><\/p>\n<p>Play the video in full screen mode for a better viewing experience.\u00a0<\/p>\n<p>Here is the code. I had to correct the possible Excel filename endings to .xls, .xlsx, or .xlsm.<\/p>\n<pre>\r\nfunction mywritetable(tableData,fileName,sheetName)\r\n% Writes table to Excel by default, but writes to CSV if too big and\r\n% appends sheetName to fileName\r\n\r\nmaxRows=1e6;\r\nif height(tableData) <  maxRows\r\n    % Default: Write to Excel (assume xls, xlsx, or xlsxm extension)\r\n    writetable(tableData,fileName,'Sheet',sheetName);\r\nelse    \r\n    % If too big: Write to a CSV file\r\n    fileName=regexprep(fileName,'(.xls|.xlsx|.xlsm)$',['_' sheetName '.csv']);\r\n    writetable(tableData,fileName);    \r\nend\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<div class=\"thumbnail thumbnail_asset asset_overlay video\"><a href=\"https:\/\/blogs.mathworks.com\/videos\/2017\/01\/05\/creating-a-function-to-save-a-table-to-a-csv-if-its-too-big-for-excel\/?dir=autoplay\"><img decoding=\"async\" src=\"https:\/\/cf-images.us-east-1.prod.boltdns.net\/v1\/static\/62009828001\/18a94a6e-2b5e-44e8-b8c7-c5e8a5a16c2a\/5ec67b83-62d7-4165-a761-24eacc566953\/640x360\/match\/image.jpg\" onError=\"this.style.display ='none';\"\/><\/p>\n<div class=\"overlay_container\">\n      <span class=\"icon-video icon_color_null\"><time class=\"video_length\">23:12<\/time><\/span>\n      <\/div>\n<p>      <\/a><\/div>\n<p>I often save tables to Excel for sharing with others, but if the data is too large, I need to share to a CSV file instead. Here I build a function to do that. Features covered in this code-along&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/videos\/2017\/01\/05\/creating-a-function-to-save-a-table-to-a-csv-if-its-too-big-for-excel\/\">read more >><\/a><\/p>\n","protected":false},"author":133,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[27,4],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts\/2827"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/users\/133"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/comments?post=2827"}],"version-history":[{"count":15,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts\/2827\/revisions"}],"predecessor-version":[{"id":2836,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts\/2827\/revisions\/2836"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/media?parent=2827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/categories?post=2827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/tags?post=2827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}