Update 6th July 2010: Walid suggests an alternative to my code in the first comment below, which apparently is much faster. I’ve not had a chance to check it out myself, but others have and like it. I suggest you use that instead of mine.
Update 1st May 2012: Pendragon found that Walid’s code didn’t work for very large data sets due to memory constraints. However, using dlmwrite to write small chunks of a large data set still gives speed advantages over my original fprintf approach. See Comments 8-12 below for details.
The Matlab csvwrite function has the limitation that it can only write numeric data to a CSV file. This is all well and good until you want a set of column headers in the first line of your file, so other people know what’s in it. I’ve seen a few workarounds for this, mostly revolving around writing out the header and CSV data as separate files then concatenating them together, either with horrible fileread calls, or (even more ugly) calling the external UNIX cat program. Either way, you end up mucking around with multiple files and associated renaming/moving commands.
Here’s a relatively easy method of writing a CSV file with a header. First, make sure you have two variables – one containing the numeric data for your file, and the other containing the header line as a string. I’m going to call my numeric data csvData and my header header, just to be original. Then I can use the following code to write both the header and csvData directly to the output file:
outid = fopen('out.csv', 'w+'); fprintf(outid, '%s', header); for i = 1:length(csvData) outLine = regexprep(num2str(csvData(i,1:37)), ' *', ','); fprintf(outid, '%s\n', outLine); end fclose(outid);
Now you’ve seen it, I’ll explain how it’s done. In the first two lines, we open a channel to a new file, and write our header as the first line. Next, we loop through each row of the data set, converting it to CSV format and adding it to a file. Finally we close the file to tidy up.
The generation of the outLine variable requires a little more examination. First off, my data set has 37 columns, so I grab them all from the ith row, and run num2str on it (There is undoubtedly a way to work out how many columns your data set has programmatically – answers in the comments please!). This gives us a single string containing all the columns, each separated by a number of spaces. Finally, we use a regular expression to convert one or more spaces into a comma – this gives us our data in the desired CSV format, ready to be added to the file.
Note: This code was developed in Matlab 6.5. I assume that the newer versions work in much the same way, but I don’t have the resources to try it.