开发者

Problem concatenating a matrix of numbers with a vector of strings (column labels) using cell2mat

开发者 https://www.devze.com 2023-03-16 18:32 出处:网络
I\'m a Mac user (10.6.8) using MATLAB to process calculation results.I output large tables of numbers to .csv files.I then use the .csv files in EXCEL.This all works fine.

I'm a Mac user (10.6.8) using MATLAB to process calculation results. I output large tables of numbers to .csv files. I then use the .csv files in EXCEL. This all works fine.

The problem is that each column of numbers needs a label (a string header). I can't figure out how to concatenate labels to the table of numbers. I would very much appreciate any advice. Here is some further informatio开发者_如何学Pythonn that might be useful:

My labels are contained within a cell array:

    columnsHeader = cell(1,15)

that I fill in with calculation results; for example:

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

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

    labelledNumbersTable=cat(1,columnsHeader,numbersTable)

I received an error that concatenated types need to be the same. So I tried converting the labels/strings using cell2mat:

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

But that took ALL the separate labels and made them into one long word... Which leads to:

??? Error using ==> cat

CAT arguments dimensions are not consistent.

Does anyone know of an alternative method that would allow me to keep my original cell array of labels?


You will have to handle writing the column headers and the numeric data to the file in two different ways. Outputting your cell array of strings will have to be done using the FPRINTF function, as described in this documentation for exporting cell arrays to text files. You can then output your numeric data by appending it to the file (which already contains the column headers) using the function DLMWRITE. Here's an example:

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


The solution to the problem is already shown by others. I am sharing a slightly different solution that improves performance especially when trying to export large datasets as CSV files.

Instead of using DLMWRITE to write the numeric data (which internally uses a for-loop over each row of the matrix), you can directly call FPRINTF to write the whole thing at once. You can see a significant improvement if the data has many rows.

Example to illustrate the difference:

%# 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

The timings on my machine were as follows:

Elapsed time is 0.786070 seconds.    %# FPRINTF
Elapsed time is 6.285136 seconds.    %# DLMWRITE
0

精彩评论

暂无评论...
验证码 换一张
取 消