File Exchange

image thumbnail

saveascii.m v6.2 (Oct 2009)

Save or display a matrix with given precision, delimiter or format for each (or every) column!

1 Download


View License

Do you still use SAVE -ASCII, DLMWRITE or DISP, NUM2STR?

With this program you can save (or display) your data with a different format and delimiters for every column, given as optional inputs in a user-friendly way (check the screenshot).

If you don't want to deal with the format (things like '%3.1f %13.4f', etc.) you can give only the precision that you want for each (or every) column (i.e. [1 4]) and the program creates the format by itself.

See this example about saving and displaying data with precision as input:

>> c = [ -130.145 45
>> -130.1 45.6789 ];
>> cfile = 'coord.dat';

>> saveascii('% My coordinates',cfile)
>> saveascii('% [LON LAT]',cfile,'a')
>> saveascii(c, cfile, 2, 'a')

>> edit(cfile)
% My coordinates
-130.15 45.00
-130.10 45.68

>> c = load(cfile);
c =
-130.1500 45.0000
-130.1000 45.6800

>> saveascii(c,1,' ') % With only 1 decimal (better than DISP)
-130.2 45.0 % and extra spaces within
-130.1 45.7

>> disp(readline(cfile))
% My coordinates
>> disp(readline(cfile,2))

Enjoy it!
Any comments and bugs reports will be very appreciated!

Comments and Ratings (18)


Arthur (view profile)

just what I needed!


Cegeon (view profile)

Just adding to the positive reviews. It's amazing.

Something that took me 59 seconds to save using dlmwrite took only 8.5 seconds, nearly SEVEN times faster! AND it can append.


Itamar (view profile)


A great code! Works great with my files. Very easy to read and undersatnd. I used to work with matlab dlmwrite, but i found many bugs while saving. I think even that your code is faster than dlmwrite. Saved me a lot of debugging time.
Great job! Thanks for sharing!

Carlos Adrián Vargas Aguilera

New rewriten version 4.0 (see the updates below). Note: must be STR2NUM instead of STR2DOUBLE up here at the description.

Karel Castro

Hola Carlos:
Muchas gracias por tu programa! fue muy util. Yo estudie en la UABC oceanologia y la maestria, ahora estoy en Inglaterra en el doctorado, y mira la sorpresa que me encuentro al buscar lo que necesitaba para mi programa, alguien de CICESE! gracias

Dan Venette

Yes! Thanks! Looking all over for something like this!

Rainer Ng

Just what I needed!

Alex Ping-Feng Chen

great job!!thanks!

Marc S

I had to debug it, but it worked fine. I use Matlab version 12. The logical operators in matlab are single & and |, not the C++ operator notation (&& and ||). The file didn't work until I did a "replace all" (|| -> |, && -> &). Then it worked great. Thanks!

Carlos Adrián Vargas Aguilera

Gracias Emilio! Ya envie la version 3.2. Tenias razon, lo cambie por chars=max(M,[],1);

El autor.

Emilio Velazquez

fantastico! justo el programa que buscaba! gracias FileExchange!

solo tuve problemas con vectores fila y cambie el inicio de la linea 355, por chars=max(M,1);

Carlos Adrian Vargas Aguilera

This is an upgraded version.
a) Now is easier to select the format (only with the decimals!) optional,
b) saves string matrices without specifying the format,
c) there's an optional output, and
d) the output file works fine for any Windows text editors.

Hope you find it useful.

Steven Bedrick

Woohoo! This saved me tons of time. Anybody know why Matlab doesn't have something this useful built right in?

Schilieri Tochiani

A program that every DATA-workman must have!

David Choi

Works great!!!! Thanks.

NOTE: saveascii will save the numbers in specified format. Matlab's save -ascii will save it in scientific notation only.

Tomas Moon

All data are save with the same specified format. Good! I always tried this with fprint but get the matrix transpose, and here's the solution. Excellent job!

urs (us) schwarz

whilst a nice snippet with good help, it emulates the well-known DLMWRITE

Frederic Moisy

Why not using Matlab's SAVE with option '-ascii' ?
(Perhaps this is explained in the help, but it is written in spanish. Ayuda en ingles por favor)



Small mistake in submission name.


v6.2 Fixed small bug with parse inputs. Now creates file directory if not exist.


v6.1 Ignores empty optional inputs. Added some comments.


v6.0 Now saves cells of strings. Fixed bug with string delimiter input.


1) Fixed SPRINTF and FPRINTF BUG with negative awful zeros!!!, like '-0.000'.
2) Modified description and screenshot above.
3) Fixed years problem on license.txt.


V5.0 Rewritten help and code. Fixed minor bugs related with empty X. No more reversed outputs. No more Beginning and Ending string optional inputs allowed. No more inputs separated by commas. New overwrite optional warning.

Fixed bug with not numeric elements (NAN's, Inf's and -Inf's).

Fix little bug with INTEGER type. Changes on the submition.

V4.0: Cleaner and rewritten code (cells), reduced running time and memmory usage, fixed bugs, accounts Inf and NaN's, change default delimiter, change beginner char, added ending option, optionaly format string output.

Bug fixed on get_digits subfunction. Used SUM of logicals instead of ANY.

Bug fixed by Emilio Velazquez

Now accept zero and negative decimals.

Now with input decimals for floating-point, 'wt' and 'at' for Windows users, output to Command Window and check errors. Some changes on the submit too.

English translation from spanish. Little changes on the submit.

Now allow different format for each column and separator.

MATLAB Release
MATLAB 7.7 (R2008b)

Inspired: readline.m v4.0

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

» Watch video

Win prizes and improve your MATLAB skills

Play today