Saving/converting cell arrays into a CVS file

2 Ansichten (letzte 30 Tage)
Simon Croft
Simon Croft am 24 Feb. 2012
I have an m*n matrix X of numerical data and want to save it to a cvs file with a 3x3 header format (i.e. 3 header rows, 3 header columns, then the data).
The matrix is big (talking >6000x6000), and the various headers are made up of the same few strings repeated in various orders.
I've started by making a cell array of the various header components,
eg header_comps={'string 1', 'string 2', 'string 3' ...}
and then from this can generate either one big cell array (header_all=cell(m+3,n+3)) with all the headers in and an empty space where X will go, or a number of individual cell arrays (row_header_1=cell(1,n), col_header_1=cell(m,1) etc.) to contain all of the required header info.
I can then either put X into the large (m+3,n+3) cell array, or have X and the various header cell arrays separate that when put together make up the desired result.
Where I am stuck is with how best to handle this data and write it to a csv file.
I am aware with a simple data matrix and row header you can use something like:
X=rand(3); %data matrix;
header=['header obj 1, header obj 2, header obj 3'];
outid = fopen('filename.csv', 'w+');
fprintf(outid, '%s', header);
fclose(outid);
dlmwrite ('filename.csv',X,'roffset',1,'-append')
which writes the char array row header into the file, then offsets the data X by one row.
I'd be happy to write 3 row headers in, 3 column headers in, then use coffset and roffset of 3. The problem is I don't seem to be able to generate the char header arrays in an automated way which I can with cell arrays, but I don't seem to be able to write cell arrays in in the same way you can a char array, and converting the cell arrays to char arrays seems to mess it up.
In short I have a list of strings that are used to generate the row and column headers. I'd like to generate row and column headers, and then put them and the data matrix in a csv file.
I've looked at a lot of solutions to writing cells to csv, but the best I've seen yet seems to be a cell by cell for-loop solution. There is a surely a more elegant way to do this?
Any help much appreciated. Regards, Simon

Antworten (0)

Kategorien

Mehr zu Cell Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by