Writing a matrix with header into a CSV/XLS file

  • Follow


Hi there!

I have a numerical matrix A, that I would like to write into a CSV or XLS file. I would like each column to have a header, so I defined the vector

headerRow = ['H1' 'H2' 'H3' 'H4' 'H5' 'H6']

and then proceeded to write it into a CSV file, followed by the matrix A:

csvwrite(fileName_XLS, headerRow)
csvwrite(fileName_XLS, A)

However this produces an empty first row! I also tried using "row,column" as extra parameters when calling csvwrite the second time, but that didn't work.

I also tried adding the heading row to the matrix and then writing the matrix as a whole to the file, but this leads to problems since the matrix then contains both numbers and strings.

I'd be very grateful if someone could recommend a way to write this matrix (+hear row) to a CSV/XLS file! Anticipated thanks.
0
Reply Catalin 5/6/2010 5:16:04 PM

Catalin Eberhardt wrote:

> I have a numerical matrix A, that I would like to write into a CSV or 
> XLS file. I would like each column to have a header, so I defined the 
> vector
> 
> headerRow = ['H1' 'H2' 'H3' 'H4' 'H5' 'H6']

> I'd be very grateful if someone could recommend a way to write this 
> matrix (+hear row) to a CSV/XLS file! Anticipated thanks.

*If* you are on MS Windows and have Excel installed, and you are 
comfortable with Excel storing your file in binary rather than as text, 
then xlwrite() of the cell array should work.

If you are on any other operating system, or you do not have Excel 
installed, or you need the file as text, then use fopen(), fprintf(), 
fclose() to write the data to a file yourself.
0
Reply Walter 5/6/2010 6:12:17 PM


> If you are on any other operating system, or you do not have Excel 
> installed, or you need the file as text, then use fopen(), fprintf(), 
> fclose() to write the data to a file yourself.

.... and you should wrap it up in a function, so you can do 

> mycsvwrite(mat, header, file)

whenever you want.  I would poke around the file exchange for that too.
0
Reply forkandwait 5/6/2010 6:36:04 PM

"Catalin Eberhardt" wrote in message <hrutgk$e9d$1@fred.mathworks.com>...
> Hi there!
> 
> I have a numerical matrix A, that I would like to write into a CSV or XLS file. I would like each column to have a header, so I defined the vector
> 
> headerRow = ['H1' 'H2' 'H3' 'H4' 'H5' 'H6']
> 
> and then proceeded to write it into a CSV file, followed by the matrix A:
> 
> csvwrite(fileName_XLS, headerRow)
> csvwrite(fileName_XLS, A)
> 
> However this produces an empty first row! I also tried using "row,column" as extra parameters when calling csvwrite the second time, but that didn't work.
> 
> I also tried adding the heading row to the matrix and then writing the matrix as a whole to the file, but this leads to problems since the matrix then contains both numbers and strings.
> 
> I'd be very grateful if someone could recommend a way to write this matrix (+hear row) to a CSV/XLS file! Anticipated thanks.

I had this same question and came up with a simple solution that doesn't involve a for loop and fprintf statement so I thought I'd share it.

Use fprintf to write a header line to a file. Then use dlmwrite to write the data to the same file as an append. For example

filename = 'test.csv';

fid = fopen(filename, 'w');
fprintf(fid, 'HEADER LINE HERE...\n');
fclose(fid)

A = rand(10,5)

dlmwrite(filename, A, '-append', 'precision', '%.6f', 'delimiter', '\t');

That worked for me. Hopefully it helps someone else with the same question.
0
Reply maowen (1) 11/12/2012 7:25:18 PM

3 Replies
635 Views

(page loaded in 0.182 seconds)

Similiar Articles:













7/26/2012 9:41:06 AM


Reply: