4.42857

4.4 | 14 ratings Rate this file 231 downloads (last 30 days) File Size: 10.69 KB File ID: #10243

saveascii.m v6.2 (Oct 2009)

by Carlos Adrian Vargas Aguilera

 

06 Mar 2006 (Updated 30 Oct 2009)

Code covered by BSD License  

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

Download Now | Watch this File

File Information
Description

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:

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

% SAVING FILE WITH COMMENTED HEADING:
>> saveascii('% My coordinates',cfile)
>> saveascii('% [LON LAT]',cfile,'a')
>> saveascii(c, cfile, 2, 'a')

% VIEWING THE RESULT:
>> edit(cfile)
---------------------------
% My coordinates
% [LON LAT]
-130.15 45.00
-130.10 45.68
---------------------------

% LOADING DATA IGNORING HEADING:
>> c = load(cfile);
c =
 -130.1500 45.0000
 -130.1000 45.6800

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

% VIEWING THE HEADING WITH readline.m
>> disp(readline(cfile))
% My coordinates
>> disp(readline(cfile,2))
% [LON LAT]

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

Acknowledgements
This submission has inspired the following:
readline.m v3.0 (Jun, 2009)
MATLAB release MATLAB 7.7 (R2008b)
Zip File Content  
Other Files license.txt,
saveascii.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (17)
08 Mar 2006 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)

08 Mar 2006 urs (us) schwarz

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

09 Mar 2006 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!

24 May 2006 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.

04 Oct 2006 Schilieri Tochiani

A program that every DATA-workman must have!

11 Dec 2006 Steven Bedrick

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

21 Feb 2007 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.

26 Feb 2007 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);

27 Feb 2007 Carlos Adrián Vargas Aguilera

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

El autor.

20 Jun 2007 Marc S

I had to debug it, but it worked fine. I use Matlab 6.0.0.88 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!

18 Sep 2007 Alex Ping-Feng Chen

great job!!thanks!

26 Sep 2007 Rainer Ng

Just what I needed!

06 Oct 2007 Dan Venette

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

05 Feb 2008 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

11 Mar 2008 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.

26 Aug 2009 Itamar

Hi,

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!

05 Nov 2009 Cegeon

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.

Please login to add a comment or rating.
Updates
28 Sep 2006

Now allow different format for each column and separator.

13 Nov 2006

English translation from spanish. Little changes on the submit.

21 Feb 2007

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.

26 Feb 2007

Now accept zero and negative decimals.

01 Mar 2007

Bug fixed by Emilio Velazquez

02 Mar 2007

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

11 Mar 2008

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.

13 Mar 2008

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

13 Mar 2008

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

06 May 2008

Fixed bug when the whole column is no finite (NaN, Inf, or -Inf's).

08 Jun 2009

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.

19 Jun 2009

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.

30 Sep 2009

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

03 Oct 2009

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

30 Oct 2009

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

30 Oct 2009

Small mistake in submission name.

Tag Activity for this File
Tag Applied By Date/Time
data import Carlos Adrian Vargas Aguilera 22 Oct 2008 08:17:31
data export Carlos Adrian Vargas Aguilera 22 Oct 2008 08:17:31
save matrix Carlos Adrian Vargas Aguilera 22 Oct 2008 08:17:31
ascii Carlos Adrian Vargas Aguilera 22 Oct 2008 08:17:31
save Carlos Adrian Vargas Aguilera 22 Oct 2008 08:17:31
fprintf Carlos Adrian Vargas Aguilera 22 Oct 2008 08:17:31
fopen Carlos Adrian Vargas Aguilera 22 Oct 2008 08:17:31
dlmwrite Carlos Adrian Vargas Aguilera 22 Oct 2008 08:17:31
csvwrite Carlos Adrian Vargas Aguilera 22 Oct 2008 08:17:31
sprintf Carlos Adrian Vargas Aguilera 22 Oct 2008 08:17:31
readline Carlos Adrian Vargas Aguilera 08 Jun 2009 15:50:26
file Carlos Adrian Vargas Aguilera 19 Jun 2009 13:22:29
ascii Net Engr 09 Sep 2009 23:07:03
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com