View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Write Cell Array to Text File

4.8 | 46 ratings Rate this file 65 Downloads (last 30 days) File Size: 2.34 KB File ID: #25387 Version: 1.2
image thumbnail

Write Cell Array to Text File


Roland Pfister (view profile)


22 Sep 2009 (Updated )

A cell array is written to a delimited output file, incl. options for appending & custom delimiters.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information

This submission works similar as Sylvain Fiedlers / Rob Kohrs "cell2csv.m". Thus, any cell array containing numbers and/or strings can be written to an output file.

dlmcell.m, however, has two main advantages:

(1) It is possible to append the cell array to an existing file.

(2) Even numbers or words that are stored as <1,1> cell within the cell array can be read.


Cell2csv inspired this file.

This file inspired Flotation Kinetics Equation Fitting, Rosin Rammler Diagram Plotting Tool, Image Quality Index Analysis Gui, Parallel Coordinate Plots Gui Toolbox, Find Core Toolbox, Version 2.0, and Cell2txtfile(File,Cell Array).

MATLAB release MATLAB 7.8 (R2009a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (56)
30 Sep 2016 Bruno Teixeira

08 Jul 2016 Gabriel Vargas

to output datetime as a string use the function datestr(datetime,formatOut), then use dlmcell and it will be compatible with matlab datetime.

Comment only
24 Jun 2016 Gabriel Vargas

great piece of code, definitely helped me solve some issues. it would be interesting if this could handle datetime and print it as a text

Comment only
23 May 2016 Osmar Charca

09 May 2016 Zoltán Gillay

05 Apr 2016 Cheng Leong Lim

20 Jan 2016 Paula Orihuela

Works perfectly! Thanks :)

20 Jan 2016 Paula Orihuela

11 Oct 2015 Arash

Arash (view profile)

It works. Nice Job, thank you.

05 Jun 2015 rmc

rmc (view profile)

01 Feb 2015 Jan Motl

Jan Motl (view profile)

It just works. Thanks!

01 Nov 2014 Jothi

Jothi (view profile)

Its word great and save more time.

Nice work.


25 Jul 2014 Scott

Scott (view profile)

Sorry for the additional post (first time posting and it looks like I can't delete posts and when you select the stars to rate the file it automatically posts!)

Anyways, ignore my comments below. I quickly realized I should just make a one cell array not nested arrays using square brackets instead of curly:

cell_array=[cells cell_nums];

With this correction it appears to work great

25 Jul 2014 Scott

Scott (view profile)

25 Jul 2014 Scott

Scott (view profile)

*Made a correction to my demo code*

If someone following this can help me I think I might cry.

With the following demo code dlmcell only puts the first 3x1 cell array into the text file and ignores the rest.

cells={'---';'aa';'boohoo hoo'};
cell_array={cells cell_nums};

I need the text output to be like so:
--- 1 2 3
aa 4 5 6
boohoo hoo 7 8 9

Any ideas?

Comment only
25 Jul 2014 Scott

Scott (view profile)

If someone following this can help me I think I might cry.

With the following demo code dlmcell only puts the first 3x1 cell array into the text file and ignores the rest.

cells={'---';'aa';'boohoo hoo'};
cell_array={a c};

I need the text output to be like so:
--- 1 2 3
aa 4 5 6
boohoo hoo 7 8 9

Any ideas?

Comment only
12 Mar 2014 Pw

Pw (view profile)

You just saved me soooo much time. Thank you!

07 Mar 2014 Mamun

Mamun (view profile)

nice work!!! kudos

04 Feb 2014 Irene

Irene (view profile)

27 Jan 2014 Fredrik

I've altered the code somewhat since the program doesn't handle cell arrays containing strings. (There's just an else option missing)

so lines 86-93 becomes:
elseif iscell(cell_array{i,j})
if size(cell_array{i,j},1) == 1 && size(cell_array{i,j},1) == 1
if isnumeric(cell_array{i,j}{1,1})
output{i,j} = num2str(cell_array{i,j}{1,1}(1,1));
elseif ischar(cell_array{i,j}{1,1})
output{i,j} = cell_array{i,j}{1,1};

output{i,j} = cell_array{i,j}{1,1};

31 Oct 2013 Frosch

Frosch (view profile)

Thank you for this nice piece of code. I had the same problem as George with the \t delimiter and changed it in a similar way, unfortunately before reading his comment ;-)Nevertheless this file saved me a lot of work!

25 Sep 2013 David

David (view profile)

Needed to output a cell to .csv file. However when I used the "cell2csv" command from the file exchange, newline characters were not recognized by notepad when I opened the csv file. The newline characters were recognized by Excel 2010 though.

MATLAB commands:
myCell = {'a';'0,1,4,4,-20,-30';'test string'}

opening cell2csv_test.csv in notepad:
a0,1,4,4,-20,-30test string

However this function did output to a .csv file with newline characters recognized by notepad. It's not a huge deal, but I was trying to make a csv file that matched a template at work *exactly* (with newlines recognizable by notepad).

MATLAB commands:
myCell = {'a';'0,1,4,4,-20,-30';'test string'}

opening dlmcell_test.csv in notepad:
test string

17 Sep 2013 Sincloe Brans

06 Aug 2013 George Papazafeiropoulos

I changed the lines 96 to 104 which were the following:

% Cell i,j is written to the output file. A delimiter is appended
% for all but the last element of each row.
if j ~= size(cell_array,2)
% At the end of a row, a newline is written to the output file.

into the following:

% Cell i,j is written to the output file. A delimiter is appended for
% all but the last element of each row. At the end of a row, a newline
% is written to the output file.
if j < size(cell_array,2)

and it runs fine. The newline written to the output file at the end of each line was erroneously left outside the for j = 1:size(cell_array,2) loop. In addition the delimiter and the '%s' strings were concatenated into a single string to provide input for the fprintf function.

Comment only
17 Jul 2013 Yuri K

Yuri K (view profile)

Why cannot you fix this annoying bug with \t?

16 Jul 2013 Tony

Tony (view profile)

03 Jun 2013 Uni A

Uni A (view profile)

Thanks for the code. I am facing a problem. Basically, I have a huge .mat file containing a list of urls. This i convert to cell format and now each row is a url, and there are around 300k rows. Now when I try to write this huge cell into a text file, it stops after around 4000 lines and says index exceeds error.

Anu suggestions please? This is kinda urgent.

Comment only
20 Mar 2013 Ricardo

works fine. thanks so much

24 Jan 2013 Robin

Robin (view profile)

10 Aug 2012 Rossella Blatt Vital

Great function! Love it!
Is there a way to add any formatting to the text? For example changing the font style (e.g., bold, oblique etc.) and the color?
Many thanks!

30 Jul 2012 Brad Stiritz

This utility doesn't work consistently for me. Specific problems:

1) Documentation states that file name is taken as the file specification. However, the function occasionally generates errors like the following:

Error using fprintf
Invalid file identifier. Use fopen to generate a valid file identifier.

Error in dlmcell (line 98)

fprintf() requires a file handle as first argument. I'll therefore have to dig into this F/E function & possibly modify.

2) This function doesn't return any values, even on error. Instead, it prints error messages at the command-line & returns normally. This is poor programming practice & makes it inconvenient to deploy within another function where error-detection must be handled programmatically.

One star subtracted for each of these problems.

24 Jul 2012 Mango

Mango (view profile)

Great work, absolutely phenomenal for what I'm trying to do. I have one problem though. In my cell array that's being printed, one of my columns has a comma in it i.e. "contract 5,000 bushels" turns into "contract 5" "000 bushels" Is there a way to fix this?

Comment only
24 Jul 2012 Mango

Mango (view profile)

Absolutely phenomenal. Only thing I could ask for would be a way to use something similar to the format %q that is available from textscan.

20 Jun 2012 Nolan Conaway


14 Jun 2012 Martin Schain

I encountered the same problem as sohrab, with \t printed in the file rather than tabs. I fixed it by replacing row 100 from
to just

Thanks for an excellent file!

08 Apr 2012 sohrab

sohrab (view profile)

\t is printed except of doing tab! what should i do?

Comment only
05 Apr 2012 Alex

Alex (view profile)

22 Mar 2012 sharsad

Cool function. But using dlmcell I could only write the first element of my cell.
My cell is in the following format;
C =
[1x5 double] [0.1962] [1x3 double] [2x3 double];
I could only write the first element of each cell content unless I need to type all the contents of my cell using the curly brace format.
Can anyone help me with this?your reply will be much appreciated

Comment only
03 Jan 2012 Matlab2010


12 Dec 2011 daisy

daisy (view profile)

Great function! We've had problem with cell2csv, thanks to dlmcell that solves our trouble!

07 Dec 2011 Evan

Evan (view profile)

This is an extremely useful script (thank you!) but I'm running into a little problem executing from Linux - slashes are appended between the fields before an after the delimiter - any ideas?

Example output text:
\Filename\,\StartDate\,\EndDate\,\Number of Measurements\

Comment only
03 Nov 2011 Michael Krause

Great function, works awesome, was clearly missing as a built-in. Well done. (gave it 5 stars, but it's not showing up in Chrome??)

13 Oct 2011 Ismael Seanez

03 Aug 2011 Jane T

Jane T (view profile)

Like Matt I was having a problem with the '\t' appearing in the file. I'm running 2011a in Windows 7.
I changed line 100 to read 'fprintf(output_file,delimiter);'

05 Jul 2011 Michael

so awesome!

28 Jun 2011 Seth

Seth (view profile)

18 May 2011 Truc Phan

works great! thanks

12 May 2011 Matt Fetterman

Matt Fetterman (view profile)

Its OK. I had a problem...could be a Mac thing. Instead of making a new line in the file, the text appeared in the file as "Hello\n", that is the special characters appeared in the file.
To get around the problem I changed the program to read:
That is, I put the special character directly in the second parameter. Then it created a new line.

21 Jan 2011 Boris

Boris (view profile)

08 Dec 2010 Rossella Blatt

Very useful! Works perfectly! Thanks.

29 Aug 2010 Ivan Brezani

perfect, thanks!

27 May 2010 hugotang Tang

This function is good, thanks, man!

03 Dec 2009 Fabio Gori

It makes my life simpler!

01 Dec 2009 Russ

Russ (view profile)

01 Nov 2009 Yonatan Cal

Thanks for this file!

28 Oct 2009 Jeff Evans

works perfectly! just as described.

12 Oct 2010 1.1

Little bugfix concerning %-signs.

07 Jan 2014 1.2

Bugfixes and corrections.

Contact us