{"id":6002,"date":"2015-05-22T09:00:13","date_gmt":"2015-05-22T13:00:13","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=6002"},"modified":"2015-08-21T13:12:16","modified_gmt":"2015-08-21T17:12:16","slug":"writing-a-cell-array-a-comparison","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2015\/05\/22\/writing-a-cell-array-a-comparison\/","title":{"rendered":"Writing A Cell Array &#8211; A Comparison"},"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 comparison of a few cell-writing functions.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Background<\/a><\/li>\r\n         <li><a href=\"#2\">Selected Entries<\/a><\/li>\r\n         <li><a href=\"#3\">Test 1: Simple<\/a><\/li>\r\n         <li><a href=\"#6\">Test 2: Mixed Data<\/a><\/li>\r\n         <li><a href=\"#9\">Test 3: Speed<\/a><\/li>\r\n         <li><a href=\"#11\">Test 4: Mixed sized Data<\/a><\/li>\r\n         <li><a href=\"#13\">Test 5: N-Dimensional Arrays<\/a><\/li>\r\n         <li><a href=\"#15\">Test 6: Help\/Flexibilty<\/a><\/li>\r\n         <li><a href=\"#21\">Comments<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Background<a name=\"1\"><\/a><\/h3>\r\n   <p>There are many high-level functions for writing common datatypes to text file formats such as <tt>writetable<\/tt>, <tt>csvwrite<\/tt>, and <tt>dlmwrite<\/tt>.  However, you might have noticed that none of these work on cell arrays.  Since cell arrays can and often do contain highly\r\n      heterogenous data, you'll often need to use lower-level functionality like <tt>fprintf<\/tt> to write them out given the knowledge of what the cell contains.  My pick today is a comparison of a few files that have\r\n      tried to automate this while trying to do the \"write\" thing.\r\n   <\/p>\r\n   <h3>Selected Entries<a name=\"2\"><\/a><\/h3>\r\n   <p>Below, I've selected a few files to compare based on a quick search and the fact that they're all covered under the BSD license:<\/p>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/25387-write-cell-array-to-text-file\">Write-cell-array-to-text-file<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1639176\">Roland Pfister<\/a> (<tt>dlmcell<\/tt>).\r\n         <\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/21800-write-a-cell-array-to-a-text-file\">Write-a-cell-array-to-a-text-file<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1269486\">Ameya Deoras<\/a> (<tt>fwritecell<\/tt>).\r\n         <\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/4400-cell-array-to-csv-file--cell2csv-m-\">Cell-array-to-csv-file--cell2csv-m<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/212470\">Sylvain Fiedler<\/a> (<tt>cell2csv<\/tt>).\r\n         <\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/47055-cell-array-to-csv-file--improved-cell2csv-m-\">Cell-array-to-csv-file--improved-cell2csv-m<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3487094\">Jerry<\/a> (<tt>impcell2csv<\/tt> renamed to avoid collision).\r\n         <\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/7363-cellwrite\">Cellwrite<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/870087\">Francis Barnhart<\/a> (<tt>cellwrite<\/tt>).\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Test 1: Simple<a name=\"3\"><\/a><\/h3>\r\n   <p>This first test is simple, write a cell array of numeric values that doesn't even have to be a cell array to a text file:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">C = num2cell(magic(5));\r\ndisp(C)<\/pre><pre style=\"font-style:oblique\">    [17]    [24]    [ 1]    [ 8]    [15]\r\n    [23]    [ 5]    [ 7]    [14]    [16]\r\n    [ 4]    [ 6]    [13]    [20]    [22]\r\n    [10]    [12]    [19]    [21]    [ 3]\r\n    [11]    [18]    [25]    [ 2]    [ 9]\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">mkdir <span style=\"color: #A020F0\">Test1<\/span>\r\ndlmcell(<span style=\"color: #A020F0\">'.\\Test1\\dlmcell.csv'<\/span>,C)\r\nfwritecell(<span style=\"color: #A020F0\">'.\\Test1\\fwritecell.csv'<\/span>,C)\r\ncell2csv(<span style=\"color: #A020F0\">'.\\Test1\\cell2csv.csv'<\/span>,C,<span style=\"color: #A020F0\">','<\/span>)\r\nimpcell2csv(<span style=\"color: #A020F0\">'.\\Test1\\impcell2csv.csv'<\/span>,C,<span style=\"color: #A020F0\">','<\/span>)\r\ncellwrite(<span style=\"color: #A020F0\">'.\\Test1\\cellwrite.csv'<\/span>,C)<\/pre><p>All of the files pass!<\/p>\r\n   <h3>Test 2: Mixed Data<a name=\"6\"><\/a><\/h3>\r\n   <p>This test mixes floating point data, integer data and string data in one cell array:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">C = {pi, <span style=\"color: #A020F0\">'Sean'<\/span>, uint8(42); uint8(42), exp(1), exp(45); <span style=\"color: #A020F0\">'MaTLaB'<\/span>,<span style=\"color: #A020F0\">'Test 2'<\/span>, 1; -1.23 -exp(45) 19};\r\ndisp(C)<\/pre><pre style=\"font-style:oblique\">    [ 3.1416]    'Sean'           [        42]\r\n    [     42]    [     2.7183]    [3.4934e+19]\r\n    'MaTLaB'     'Test 2'         [         1]\r\n    [-1.2300]    [-3.4934e+19]    [        19]\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">mkdir <span style=\"color: #A020F0\">Test2<\/span>\r\ndlmcell(<span style=\"color: #A020F0\">'.\\Test2\\dlmcell.csv'<\/span>,C)\r\nfwritecell(<span style=\"color: #A020F0\">'.\\Test2\\fwritecell.csv'<\/span>,C)\r\ncell2csv(<span style=\"color: #A020F0\">'.\\Test2\\cell2csv.csv'<\/span>,C,<span style=\"color: #A020F0\">','<\/span>)\r\nimpcell2csv(<span style=\"color: #A020F0\">'.\\Test2\\impcell2csv.csv'<\/span>,C,<span style=\"color: #A020F0\">','<\/span>)\r\ncellwrite(<span style=\"color: #A020F0\">'.\\Test2\\cellwrite.csv'<\/span>,C)<\/pre><p><tt>fwritecell<\/tt> was unable to resolve this cell array because it needs consistent formatting.  Because it relies on <tt>cell2mat<\/tt>, this continues to pop up.  The others pass.\r\n   <\/p>\r\n   <h3>Test 3: Speed<a name=\"9\"><\/a><\/h3>\r\n   <p>This test is for speed.  I'm going to write a large cell array to see what happens.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">C = repmat(C,1000,10);\r\ndisp(<span style=\"color: #A020F0\">'Size of C'<\/span>)\r\ndisp(size(C))<\/pre><pre style=\"font-style:oblique\">Size of C\r\n        4000          30\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">mkdir <span style=\"color: #A020F0\">Test3<\/span>\r\nt = nan(1,5);\r\ntic\r\ndlmcell(<span style=\"color: #A020F0\">'.\\Test3\\dlmcell.csv'<\/span>,C)\r\nt(1) = toc;\r\ntic\r\ncell2csv(<span style=\"color: #A020F0\">'.\\Test3\\cell2csv.csv'<\/span>,C,<span style=\"color: #A020F0\">','<\/span>)\r\nt(3) = toc;\r\ntic\r\nimpcell2csv(<span style=\"color: #A020F0\">'.\\Test3\\impcell2csv.csv'<\/span>,C,<span style=\"color: #A020F0\">','<\/span>)\r\nt(4) = toc;\r\ntic\r\ncellwrite(<span style=\"color: #A020F0\">'.\\Test3\\cellwrite.csv'<\/span>,C)\r\nt(5) = toc\r\n<\/pre><pre>t = <br> 11.2884 NaN 16.8843 8.3786 7.4123<\/pre><p><tt>cellwrite<\/tt> is the winner by just under a second over <tt>impcell2csv<\/tt>!\r\n   <\/p>\r\n   <h3>Test 4: Mixed sized Data<a name=\"11\"><\/a><\/h3>\r\n   <p>Here we will have a different sized cells.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">C = num2cell(magic(2));\r\nC{2} = C;\r\nC{3} = <span style=\"color: #A020F0\">'intlinprog'<\/span>;\r\ndisp(C)<\/pre><pre style=\"font-style:oblique\">    [       1]    'intlinprog'\r\n    {2x2 cell}    [         2]\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">mkdir <span style=\"color: #A020F0\">Test4<\/span>\r\ndlmcell(<span style=\"color: #A020F0\">'.\\Test4\\dlmcell.csv'<\/span>,C)\r\ncell2csv(<span style=\"color: #A020F0\">'.\\Test4\\cell2csv.csv'<\/span>,C,<span style=\"color: #A020F0\">','<\/span>)\r\nimpcell2csv(<span style=\"color: #A020F0\">'.\\Test4\\impcell2csv.csv'<\/span>,C,<span style=\"color: #A020F0\">','<\/span>)\r\ncellwrite(<span style=\"color: #A020F0\">'.\\Test4\\cellwrite.csv'<\/span>,C)<\/pre><p>The winner here is going to be <tt>impcell2csv<\/tt> with <tt>dlmcell<\/tt> coming in a close second.  <tt>impcell2csv<\/tt> wrote NA in the spot of the nested cell, <tt>dlmcell<\/tt> left it blank.  The others gave non-descript errors with the file partially written.\r\n   <\/p>\r\n   <p><b>Challenge<\/b> Does anyone know of a file that will handle this?  Perhaps padding with extra commas or providing other options?\r\n   <\/p>\r\n   <h3>Test 5: N-Dimensional Arrays<a name=\"13\"><\/a><\/h3>\r\n   <p>Here we will have a three-d cell array.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">C = {pi, <span style=\"color: #A020F0\">'Sean'<\/span>, uint8(42); uint8(42), exp(1), exp(45); <span style=\"color: #A020F0\">'MaTLaB'<\/span>,<span style=\"color: #A020F0\">'Test 2'<\/span>, 1; -1.23 -exp(45) 19};\r\nC = cat(3,C,C);\r\ndisp(C)<\/pre><pre style=\"font-style:oblique\">(:,:,1) = \r\n    [ 3.1416]    'Sean'           [        42]\r\n    [     42]    [     2.7183]    [3.4934e+19]\r\n    'MaTLaB'     'Test 2'         [         1]\r\n    [-1.2300]    [-3.4934e+19]    [        19]\r\n(:,:,2) = \r\n    [ 3.1416]    'Sean'           [        42]\r\n    [     42]    [     2.7183]    [3.4934e+19]\r\n    'MaTLaB'     'Test 2'         [         1]\r\n    [-1.2300]    [-3.4934e+19]    [        19]\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">mkdir <span style=\"color: #A020F0\">Test5<\/span>\r\ndlmcell(<span style=\"color: #A020F0\">'.\\Test5\\dlmcell.csv'<\/span>,C)\r\ncell2csv(<span style=\"color: #A020F0\">'.\\Test5\\cell2csv.csv'<\/span>,C,<span style=\"color: #A020F0\">','<\/span>)\r\nimpcell2csv(<span style=\"color: #A020F0\">'.\\Test5\\impcell2csv.csv'<\/span>,C,<span style=\"color: #A020F0\">','<\/span>)\r\ncellwrite(<span style=\"color: #A020F0\">'.\\Test5\\cellwrite.csv'<\/span>,C)<\/pre><p><tt>impcell2csv<\/tt> and <tt>cellwrite<\/tt> did the same thing here, append the second page to the right of the first page.  I guess that's a tie, <tt>cell2csv<\/tt> and <tt>dlmcell<\/tt> both ignored the second page altogether.\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/maincell2csv\/cell2csv.png\"> <\/p>\r\n   <h3>Test 6: Help\/Flexibilty<a name=\"15\"><\/a><\/h3>\r\n   <p>How's the help\/flexibility?<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">help <span style=\"color: #A020F0\">dlmcell<\/span><\/pre><pre style=\"font-style:oblique\">  &lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt; %%\r\n  &lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;     dlmcell - Write Cell Array to Text File      &lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt; %\r\n  &lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt; %\r\n                                                  Version:    01.06.2010 %\r\n                                                      (c) Roland Pfister %\r\n                                              roland_pfister@t-online.de %\r\n                         ...with many thanks to George Papazafeiropoulos %\r\n                         for his corrections and improvements.           %\r\n  1. Synopsis                                                            %\r\n                                                                         %\r\n  A single cell array is written to an output file. Cells may consist of %\r\n  any combination of (a) numbers, (b) letters, or (c) words. The inputs  %\r\n  are as follows:                                                        %\r\n                                                                         %\r\n        - file       The output filename (string).                       %\r\n        - cell_array The cell array to be written.                       %\r\n        - delimiter  Delimiter symbol, e.g. ',' (optional;               %\r\n                     default: tab ('\\t'}).                               %\r\n        - append     '-a' for appending the content to the               %\r\n                     output file (optional).                             %\r\n                                                                         %\r\n  2. Example                                                             %\r\n                                                                         %\r\n          mycell = {'Numbers', 'Letters', 'Words','More Words'; ...      %\r\n                     1, 'A', 'Apple', {'Apricot'}; ...                   %\r\n                     2, 'B', 'Banana', {'Blueberry'}; ...                %\r\n                     3, 'C', 'Cherry', {'Cranberry'}; };                 %\r\n          dlmcell('mytext.txt',mycell);                                  %\r\n                                                                         %\r\n  &lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt; %\r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">help <span style=\"color: #A020F0\">fwritecell<\/span><\/pre><pre style=\"font-style:oblique\">  FWRITECELL writes formatted data from a cell array to a text file.\r\n \r\n  fwritecell(filename, format, data) \r\n      writes data using the C language conversion specifications in\r\n      variable \"format\" as with SPRINTF\r\n      Example: fwritecell('textfile1.txt','%2d %1d %21s %8.5f',C);\r\n      \r\n  fwritecell(filename, data)\r\n      writes data using a fixed width format padded with whitespace. Note \r\n      that the decimal point for floating point numbers may not be aligned\r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">help <span style=\"color: #A020F0\">cell2csv<\/span><\/pre><pre style=\"font-style:oblique\">  Writes cell array content into a *.csv file.\r\n  \r\n  CELL2CSV(fileName, cellArray, separator, excelYear, decimal)\r\n \r\n  fileName     = Name of the file to save. [ i.e. 'text.csv' ]\r\n  cellArray    = Name of the Cell Array where the data is in\r\n  separator    = sign separating the values (default = ';')\r\n  excelYear    = depending on the Excel version, the cells are put into\r\n                 quotes before they are written to the file. The separator\r\n                 is set to semicolon (;)\r\n  decimal      = defines the decimal separator (default = '.')\r\n \r\n          by Sylvain Fiedler, KA, 2004\r\n  updated by Sylvain Fiedler, Metz, 06\r\n  fixed the logical-bug, Kaiserslautern, 06\/2008, S.Fiedler\r\n  added the choice of decimal separator, 11\/2010, S.Fiedler\r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">help <span style=\"color: #A020F0\">impcell2csv<\/span><\/pre><pre style=\"font-style:oblique\">  % Writes cell array content into a *.csv file.\r\n  % \r\n  % CELL2CSV(fileName, cellArray[, separator, excelYear, decimal])\r\n  %\r\n  % fileName     = Name of the file to save. [ e.g. 'text.csv' ]\r\n  % cellArray    = Name of the Cell Array where the data is in\r\n  % \r\n  % optional:\r\n  % separator    = sign separating the values (default = ',')\r\n  % excelYear    = depending on the Excel version, the cells are put into\r\n  %                quotes before they are written to the file. The separator\r\n  %                is set to semicolon (;)  (default = 1997 which does not change separator to semicolon ;)\r\n  % decimal      = defines the decimal separator (default = '.')\r\n  %\r\n  %         by Sylvain Fiedler, KA, 2004\r\n  % updated by Sylvain Fiedler, Metz, 06\r\n  % fixed the logical-bug, Kaiserslautern, 06\/2008, S.Fiedler\r\n  % added the choice of decimal separator, 11\/2010, S.Fiedler\r\n  % modfiedy and optimized by Jerry Zhu, June, 2014, jerryzhujian9@gmail.com\r\n  % now works with empty cells, numeric, char, string, row vector, and logical cells. \r\n  % row vector such as [1 2 3] will be separated by two spaces, that is \"1  2  3\"\r\n  % One array can contain all of them, but only one value per cell.\r\n  % 2x times faster than Sylvain's codes (8.8s vs. 17.2s):\r\n  % tic;C={'te','tm';5,[1,2];true,{}};C=repmat(C,[10000,1]);cell2csv([datestr(now,'MMSS') '.csv'],C);toc;\r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">help <span style=\"color: #A020F0\">cellwrite<\/span><\/pre><pre style=\"font-style:oblique\"> CELLWRITE Write a cell array to a comma separated value file.\r\n    CELLWRITE(FILENAME, C) writes cell array C into FILENAME as comma\r\n    separated values.\r\n \r\n    NOTE: This function is not completely compatible with CSVWRITE.\r\n    Offsets are not supported and 0 values are not omitted.\r\n \r\n    See also CSVWRITE, CSVREAD, DLMREAD, DLMWRITE, WK1READ, WK1WRITE.\r\n\r\n<\/pre><p>The help for all of them is good.  I like <tt>dlmcell<\/tt>'s ability to append, and I like <tt>impcell2csv<\/tt>'s description of the enhancements though the help is mostly borrowed from <tt>cell2csv<\/tt>.\r\n   <\/p>\r\n   <h3>Comments<a name=\"21\"><\/a><\/h3>\r\n   <p>Do you use cell arrays to store data?  If so, do you need to write them out to text often?  Have you ever written your own\r\n      <tt>fprintf<\/tt> wrapper or used one of the above files or another that I didn't see?  Let us know <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=6002#respond\">here<\/a> or leave a comment for one of the authors above on their File Exchange entry page.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_14d63d40ddef4274bb4fdbd074dc23d1() {\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='14d63d40ddef4274bb4fdbd074dc23d1 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 14d63d40ddef4274bb4fdbd074dc23d1';\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_14d63d40ddef4274bb4fdbd074dc23d1()\"><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\n14d63d40ddef4274bb4fdbd074dc23d1 ##### SOURCE BEGIN #####\r\n%% CELL2CSV - A Comparison\r\n%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495 Sean>'s\r\n% pick this week is a comparison of a few cell-writing functions.\r\n% \r\n\r\n%% Background\r\n% There are many high-level functions for writing common datatypes to text\r\n% file formats such as |writetable|, |csvwrite|, and |dlmwrite|.  However,\r\n% you might have noticed that none of these work on cell arrays.  Since\r\n% cell arrays can and often do contain highly heterogenous data, you'll often\r\n% need to use lower-level functionality like |fprintf| to write them out\r\n% given the knowledge of what the cell contains.  My pick today is a\r\n% comparison of a few files that have tried to automate this while trying to do the\r\n% \"write\" thing.\r\n%\r\n%% Selected Entries\r\n% Below, I've selected a few files to compare based on a quick search and the\r\n% fact that they're all covered under the BSD license:\r\n%\r\n% * <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/25387-write-cell-array-to-text-file\r\n% Write-cell-array-to-text-file> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1639176 Roland\r\n% Pfister> (|dlmcell|).\r\n% * <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/21800-write-a-cell-array-to-a-text-file\r\n% Write-a-cell-array-to-a-text-file> by <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1269486 Ameya Deoras> (|fwritecell|).\r\n% * <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/4400-cell-array-to-csv-file--cell2csv-m-\r\n% Cell-array-to-csv-fileREPLACE_WITH_DASH_DASHcell2csv-m> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/212470 Sylvain Fiedler> (|cell2csv|).\r\n% * <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/47055-cell-array-to-csv-file--improved-cell2csv-m-\r\n% Cell-array-to-csv-fileREPLACE_WITH_DASH_DASHimproved-cell2csv-m> by <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3487094 Jerry> (|impcell2csv| renamed to avoid collision).\r\n% * <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/7363-cellwrite Cellwrite> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/870087 Francis Barnhart> (|cellwrite|).\r\n%\r\n\r\n%% Test 1: Simple\r\n% This first test is simple, write a cell array of numeric values that\r\n% doesn't even have to be a cell array to a text file:\r\n\r\nC = num2cell(magic(5)); \r\ndisp(C)\r\n\r\n%%\r\n%   mkdir Test1\r\n%   dlmcell('.\\Test1\\dlmcell.csv',C)\r\n%   fwritecell('.\\Test1\\fwritecell.csv',C)\r\n%   cell2csv('.\\Test1\\cell2csv.csv',C,',')\r\n%   impcell2csv('.\\Test1\\impcell2csv.csv',C,',')\r\n%   cellwrite('.\\Test1\\cellwrite.csv',C)\r\n\r\n%%\r\n% All of the files pass!\r\n\r\n%% Test 2: Mixed Data\r\n% This test not mixes floating point data, integer data and string data in\r\n% one cell array:\r\n\r\nC = {pi, 'Sean', uint8(42); uint8(42), exp(1), exp(45); 'MaTLaB','Test 2', 1; -1.23 -exp(45) 19};\r\ndisp(C)\r\n\r\n%%\r\n%   mkdir Test2\r\n%   dlmcell('.\\Test2\\dlmcell.csv',C)\r\n%   fwritecell('.\\Test2\\fwritecell.csv',C)\r\n%   cell2csv('.\\Test2\\cell2csv.csv',C,',')\r\n%   impcell2csv('.\\Test2\\impcell2csv.csv',C,',')\r\n%   cellwrite('.\\Test2\\cellwrite.csv',C)\r\n\r\n%%\r\n% |fwritecell| was unable to resolve this cell array because it needs\r\n% consistent formatting.  Because it relies on |cell2mat|, this continues\r\n% to pop up.  The others pass.\r\n\r\n%% Test 3: Speed\r\n% This test is for speed.  I'm going to write a large cell array to\r\n% see what happens.\r\n\r\nC = repmat(C,1000,10);\r\ndisp('Size of C')\r\ndisp(size(C))\r\n\r\n%%\r\n%   mkdir Test3\r\n%   t = nan(1,5);\r\n%   tic\r\n%   dlmcell('.\\Test3\\dlmcell.csv',C)\r\n%   t(1) = toc;\r\n%   tic\r\n%   cell2csv('.\\Test3\\cell2csv.csv',C,',')\r\n%   t(3) = toc;\r\n%   tic\r\n%   impcell2csv('.\\Test3\\impcell2csv.csv',C,',')\r\n%   t(4) = toc;\r\n%   tic\r\n%   cellwrite('.\\Test3\\cellwrite.csv',C)\r\n%   t(5) = toc;\r\n%   disp(t)\r\n%\r\n%  t = 11.2884 NaN 16.8843 8.3786 7.4123\r\n%\r\n% |cellwrite| is the winner by just under a second over |impcell2csv|!\r\n\r\n%% Test 4: Mixed sized Data\r\n% Here we will have a different sized cells.\r\nC = num2cell(magic(2)); \r\nC{2} = C; \r\nC{3} = 'intlinprog';\r\ndisp(C)\r\n\r\n%%\r\n%   mkdir Test4\r\n%   dlmcell('.\\Test4\\dlmcell.csv',C)\r\n%   cell2csv('.\\Test4\\cell2csv.csv',C,',')\r\n%   impcell2csv('.\\Test4\\impcell2csv.csv',C,',')\r\n%   cellwrite('.\\Test4\\cellwrite.csv',C)\r\n%\r\n% The winner here is going to be |impcell2csv| with |dlmcell| coming in a\r\n% close second.  |impcell2csv| wrote NA in the spot of the nested cell,\r\n% |dlmcell| left it blank.  The others gave non-descript errors with\r\n% the file partially written.  \r\n% \r\n% *Challenge* Does anyone know of a file that will handle this?  Perhaps\r\n% padding with extra commas or providing other options?\r\n\r\n%% Test 5: N-Dimensional Arrays\r\n% Here we will have a three-d cell array.\r\nC = {pi, 'Sean', uint8(42); uint8(42), exp(1), exp(45); 'MaTLaB','Test 2', 1; -1.23 -exp(45) 19};\r\nC = cat(3,C,C);\r\ndisp(C)\r\n\r\n%%\r\n%   mkdir Test5\r\n%   dlmcell('.\\Test5\\dlmcell.csv',C)\r\n%   cell2csv('.\\Test5\\cell2csv.csv',C,',')\r\n%   impcell2csv('.\\Test5\\impcell2csv.csv',C,',')\r\n%   cellwrite('.\\Test5\\cellwrite.csv',C)\r\n%\r\n% |impcell2csv| and |cellwrite| did the same thing here, append the second\r\n% page to the right of the first page.  I guess that's a tie, |cell2csv| and\r\n% |dlmcell| both ignored the second page altogether.\r\n%\r\n% <<cell2csv.png>>\r\n\r\n%% Test 6: Help\/Flexibilty\r\n% How's the help\/flexibility?\r\n\r\nhelp dlmcell\r\n%%\r\nhelp fwritecell\r\n%%\r\nhelp cell2csv\r\n%%\r\nhelp impcell2csv\r\n%%\r\nhelp cellwrite\r\n\r\n%%\r\n% The help for all of them is good.  I like |dlmcell|'s ability to append,\r\n% and I like |impcell2csv|'s description of the enhancements though the\r\n% help is mostly borrowed from |cell2csv|.\r\n\r\n\r\n%% Comments\r\n% \r\n% Do you use cell arrays to store data?  If so, do you need to write them\r\n% out to text often?  Have you ever written your own |fprintf| wrapper or\r\n% used one of the above files or another that I didn't see?  Let us know\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=6002#respond here> or leave a comment\r\n% for one of the authors above on their File Exchange entry page.\r\n\r\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/maincell2csv\/cell2csv.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n   \r\n      Sean's pick this week is a comparison of a few cell-writing functions.\r\n      \r\n   \r\n   Contents\r\n   \r\n      \r\n       ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2015\/05\/22\/writing-a-cell-array-a-comparison\/\">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],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6002"}],"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=6002"}],"version-history":[{"count":6,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6002\/revisions"}],"predecessor-version":[{"id":6142,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6002\/revisions\/6142"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=6002"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=6002"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=6002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}