4.8

4.8 | 36 ratings Rate this file 140 Downloads (last 30 days) File Size: 2.34 KB File ID: #25387
image thumbnail

Write Cell Array to Text File

by

 

22 Sep 2009 (Updated )

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

| Watch this File

File Information
Description

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.

Acknowledgements

Cell2csv inspired this file.

This file inspired Flotation Kinetics Equation Fitting, Rosin Rammler Diagram Plotting Tool, Image Quality Index Analysis Gui, and Parallel Coordinate Plots Gui Toolbox.

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 (44)
25 Jul 2014 Scott

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];
dlmcell('mytext.txt',cell_array);

With this correction it appears to work great

25 Jul 2014 Scott  
25 Jul 2014 Scott

*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'};
nums=[1,2,3;4,5,6;7,8,9];
cell_nums=num2cell(nums);
cell_array={cells cell_nums};
dlmcell('mytext.txt',cell_array{:});

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

Any ideas?
Thanks!

25 Jul 2014 Scott

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'};
nums=[1,2,3;4,5,6;7,8,9];
cell_nums=num2cell(nums);
cell_array={a c};
dlmcell('mytext.txt',cell_array);

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

Any ideas?
Thanks!

12 Mar 2014 Pw

You just saved me soooo much time. Thank you!

07 Mar 2014 Mamun

nice work!!! kudos

04 Feb 2014 Irene  
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};
end;

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

31 Oct 2013 Frosch

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

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'}
cell2csv('cell2csv_test.csv',myCell)

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'}
dlmcell('dlmcell_test.csv',myCell)

opening dlmcell_test.csv in notepad:
a
0,1,4,4,-20,-30
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.
fprintf(output_file,'%s',output{i,j});
if j ~= size(cell_array,2)
fprintf(output_file,'%s',delimiter);
end
end;
% At the end of a row, a newline is written to the output file.
fprintf(output_file,'\r\n');

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)
fprintf(output_file,['%s',delimiter],output{i,j});
else
fprintf(output_file,'%s\r\n',output{i,j});
end
end;

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.

17 Jul 2013 Yuri K

Why cannot you fix this annoying bug with \t?

16 Jul 2013 Tony  
03 Jun 2013 Uni A

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.

20 Mar 2013 Ricardo

works fine. thanks so much

24 Jan 2013 Robin  
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(output_file,'%s',output{i,j});
------------------------------

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

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?

24 Jul 2012 Mango

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 C

Nice!

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
>>fprintf(output_file,'%s',delimiter);
to just
>>fprintf(output_file,delimiter);

Thanks for an excellent file!

08 Apr 2012 sohrab

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

05 Apr 2012 Alex  
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

03 Jan 2012 none

excellent.

12 Dec 2011 daisy

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

07 Dec 2011 Evan

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\

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

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  
18 May 2011 Truc Phan

works great! thanks

12 May 2011 Matt Fetterman

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:
fprintf(fid,'%s\n',astring)
That is, I put the special character directly in the second parameter. Then it created a new line.

21 Jan 2011 Boris  
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  
01 Nov 2009 Yonatan Cal

Thanks for this file!

28 Oct 2009 Jeff Evans

works perfectly! just as described.

Updates
12 Oct 2010

Little bugfix concerning %-signs.

07 Jan 2014

Bugfixes and corrections.

Contact us