comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: Writing CSV files?
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: Writing CSV files? [message #37522] Tue, 06 January 2004 14:12 Go to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Jonathan Greenberg writes:

> Is there an easy way to write an array to an ASCII comma deliminated file
> (CSV)? I see a READ_ASCII command but no WRITE_ASCII. Thanks!

Here is an article that discusses one way to do this:

http://www.dfanning.com/tips/csv_file.html

You will find a reference there to a program named
WRITE_CSV_DATA that will write a 2D array and an
optional vector of column headers to a CSV file.

You might also think about WRITE_SYLK if you are trying
to write a file that can be opened in a spreadsheet.

Cheers,

David

--
David W. Fanning, Ph.D.
Fanning Software Consulting, Inc.
Phone: 970-221-0438, E-mail: david@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
Re: Writing CSV files? [message #37606 is a reply to message #37522] Thu, 08 January 2004 01:46 Go to previous message
Timm Weitkamp is currently offline  Timm Weitkamp
Messages: 66
Registered: August 2002
Member
Format codes provide a convenient way of bringing numbers in IDL into
shape for output to screen or files (see also "Format Codes" in the online
help). Why not use them in a WRITE_CSV routine, like this simple one:

PRO write_csv, fileName, data

nColumns = (SIZE(data))[1]
formatStr = '(' + STRTRIM(nColumns,2) + '(F, :,", "))'

OPENW, u, fileName, /GET_LUN, WIDTH=1000
PRINTF, u, data, FORMAT=formatStr
FREE_LUN, u

END

If the data arrays you want to write are very large, then you may want to
replace the single "PRINTF, ..." line above by a FOR loop to avoid memory
problems:

[...]
OPENW, u, fileName, /GET_LUN, WIDTH=1000
nRows = (SIZE(data))[2]
FOR iRow = 0, nRows-1 DO BEGIN
thisRow = STRING(data[*,iRow], FORMAT=formatStr)
PRINTF, u, thisRow
ENDFOR
FREE_LUN, u
[...]

Hope this helps,

Timm

--
Timm Weitkamp <http://people.web.psi.ch/weitkamp>


On 06.01.04 at 15:12 -0700, David Fanning wrote:

> Jonathan Greenberg writes:
>
>> Is there an easy way to write an array to an ASCII comma deliminated file
>> (CSV)? I see a READ_ASCII command but no WRITE_ASCII. Thanks!
>
> Here is an article that discusses one way to do this:
>
> http://www.dfanning.com/tips/csv_file.html
>
> [...]
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Maximum likelihood question (ENVI)
Next Topic: Writing CSV files?

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 17:25:29 PDT 2025

Total time taken to generate the page: 0.00599 seconds