Concatenation of a matrix of numbers with a row vector (column labels) using cell2mat

I am a Mac user (10.6.8), using MATLAB to process calculation results. I output large numbers tables to CSV files. Then I use CSV files in EXCEL. All of this works great.

The problem is that each column of numbers needs a label (row header). I cannot figure out how to combine labels into a number table. I would really appreciate any advice. Here is some additional information that may be helpful:

My labels are contained in an array of cells:

    columnsHeader = cell(1,15)

which I fill in with the calculation results; eg:

    columnsHeader{1}  = propertyStringOne (where propertyStringOne = 'Liq')

The sequence of labels is different for each calculation. My first attempt was to try to directly connect the labels:

    labelledNumbersTable=cat(1,columnsHeader,numbersTable)

, . / cell2mat:

    columnsHeader = cell2mat(columnsHeader);
    labelledNumbersTable = cat(1,columnsHeader,numbersTable)

... :

??? == > cat

CAT .

- , ?

+5
2

. FPRINTF, . , ( ), DLMWRITE. :

fid = fopen('myfile.csv','w');              %# Open the file
fprintf(fid,'%s,',columnsHeader{1:end-1});  %# Write all but the last label
fprintf(fid,'%s\n',columnsHeader{end});     %# Write the last label and a newline
fclose(fid);                                %# Close the file
dlmwrite('myfile.csv',numbersTable,'-append');  %# Append your numeric data
+6

. , , CSV.

DLMWRITE ( for ), FPRINTF, . , .

, :

%# some random data with column headers
M = rand(100000,5);                                          %# 100K rows, 5 cols
H = strtrim(cellstr( num2str((1:size(M,2))','Col%d') ));     %'# headers

%# FPRINTF
tic
fid = fopen('a.csv','w');
fprintf(fid,'%s,',H{1:end-1});
fprintf(fid,'%s\n',H{end});
fprintf(fid, [repmat('%.5g,',1,size(M,2)-1) '%.5g\n'], M');  %'# default prec=5
fclose(fid);
toc

%# DLMWRITE
tic
fid = fopen('b.csv','w');
fprintf(fid,'%s,',H{1:end-1});
fprintf(fid,'%s\n',H{end});
fclose(fid);
dlmwrite('b.csv', M, '-append');
toc

:

Elapsed time is 0.786070 seconds.    %# FPRINTF
Elapsed time is 6.285136 seconds.    %# DLMWRITE
+6

All Articles