Writing CSV files with headers in Matlab
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.
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.
April 11, 2010
Tags:
apps, matlab




6 Responses
Walid - July 5, 2010
The code is a bit slow, the following will greatly expedite it
header=['test 1, test 2, test 3'];
outid = fopen(‘out.csv’, ‘w+’);
fprintf(outid, ‘%s’, header);
fclose(outid);
dlmwrite (‘out.csv’,csvData,’roffset’,1,’-append’)
squaregoldfish - July 6, 2010
Thanks for the suggestion. I’ll try it out next time I need it!
Steve.
simongui - November 9, 2010
works great. thanks,
squaregoldfish - November 9, 2010
Out of interest, did you use my code or Walid’s? I’ve never got round to trying his version…
markus - June 10, 2011
thanks a lot, walids code is great!
Chandra Kiran - December 19, 2011
Hi,
I used Walid’s code, and it worked perfectly well. Thanks are due to both Squaregoldfish and to Walid for posting this vry useful bit of code.
Leave a Reply