Write a .csv file with Save operation

I want to save data as a true .csv file, i.e., comma-delimited values not tab-delimited as is currently made with save/J

The Save operation gives control over what to use for line-breaks, but it would be nice to have control over the character to use for the value separator as well. Maybe a /J = "separator char" option?

I recently learned of the SaveTableCopy operation, which does have a .csv option, and can be used in many situations, but seems to be a bit less convenient than save.
I plan to add comma support but until I get to it, here is a procedure.

Out of curiosity, why do you want comma-separated rather than tab-separated?

hrodstein wrote:
I plan to add comma support but until I get to it, here is a procedure.

Out of curiosity, why do you want comma-separated rather than tab-separated?



Thanks Howard for the procedure. My quick'n'dirty solution was to simply make a table from the list of waves I wanted to save, and call SaveTableCopy.
// saveList is a string containing a semicolon-separated list of names of waves to save
// sharedWavesPath is a string containing the name of an existing Igor Path
// fileName is a string containing the default name for the file to be saved
edit/N=SaveCsvTable  as "Save csv Table"
string tableName = S_name
variable nCOls = itemsInList (saveList, ";")
for (iCol = 0; iCol < nCols; iCol +=1)
    wave aWave = $StringFromList (iCol, SaveList, ";")
    appendToTable/W=$tableName aWave
    if (cmpstr (waveUnits(aWave, -1), "dat") ==0)
        ModifyTable/W=$tableName format(aWave)=8
    elseif (wavetype (aWave) == 4)
        ModifyTable /W= $tableName sigDigits(aWave)=16
    else
        ModifyTable /W= $tableName sigDigits(aWave)=8
    endif
endfor
doUpdate
SaveTableCopy /I/M="\r\n"/N=1/O/P=$sharedWavesPath /S=0/T=2/W= $tableName as fileName + ".csv"
killwindow $tableName


As to why csv is a desirable addition, maybe I am wrong, but it seems that .csv files are a bit more of a standard then tab-delimited. I'm thinking of the less savvy user here, but if I send someone (who may not have Igor - I already said they were less savvy) a .csv file, chances are they can double-click it and have it open as a spread-sheet, whereas a tab-delimited file with .txt extension may open as plain text, or may lead to a dialog asking how to load the file, and the file extension, .txt offers no clues as to how to do that.

On a tangentially related note, if a tab-delimited file is given the .xls extension of an MS Excel file, Excel will open it properly when it is double clicked, just as if it were a real Excel file.

Dr. Jamie Boyd, Ph.D.
UBC In Vivo Imaging Core
Quote:
As to why csv is a desirable addition, maybe I am wrong, but it seems that .csv files are a bit more of a standard then tab-delimited.


I was wondering if the request for CSV (you are not the first) arose because there are commonly-used data analysis programs that accept CSV but not tab-delimited.

Quote:
I'm thinking of the less savvy user here, but if I send someone (who may not have Igor - I already said they were less savvy) a .csv file, chances are they can double-click it and have it open as a spread-sheet, whereas a tab-delimited file with .txt extension may open as plain text, or may lead to a dialog asking how to load the file, and the file extension, .txt offers no clues as to how to do that.


I see. .dat might work for tab-delimited.

Quote:
On a tangentially related note, if a tab-delimited file is given the .xls extension of an MS Excel file, Excel will open it properly when it is double clicked, just as if it were a real Excel file.


Yes. This has caused problems. People try to open .xls files with XLLoadWave and are told "this is not an Excel binary file". They don't understand that so they ask for help from WaveMetrics support. They send the file and I determine that it is, in fact, a plain text file. This happens every once-in-a-while and may happen more often than that in the wild.

It took me a while before I understood that programs create plain-text files and use the .xls extension so they will open in Excel.

This led me to add code to XLLoadWave that inspects the file and, if it appears to be a text file, displays the message "This appears to be a text file. Inspect it with a text editor." Now I rarely get that support question.

Thus are the ills of the universe conquered one-by-one :)





While its been a long while since I have done this, I know that CSV files also import nicely via a database package in LaTeX as a way to create nicely formatted tables.

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville