How can I save this cell
data = cell([3 2]);
data{1,1} = 'Bla';
data{2,1} = 'Bla1';
data{3,1} = 'Bla2';
data{1,2} = '2';
data{2,2} = '3';
data{3,2} = '1'
to a csv file?
I tried
dlmwrite('C:\Users\Ro\Desktop\Mestrado\Resultados\Tabelas\VaR_tab.csv',data,'delimiter',';')
but it give this error message:
Error using dlmwrite (line 118)
The input cell array cannot be converted to a matrix.
csvwrite( filename , M ) writes matrix M to file filename as comma-separated values. csvwrite( filename , M , row , col ) writes matrix M to file filename starting at the specified row and column offset. The row and column arguments are zero based, so that row=0 and col=0 specify the first value in the file.
Append Cell Array Below Existing Data in SpreadsheetAppend a cell array to the bottom of a spreadsheet file containing existing data. Create two cell arrays in the workspace. Write the cell array C to a spreadsheet file called C .
You can export a cell array from MATLAB® workspace into a text file in one of these ways: Use the writecell function to export the cell array to a text file. Use fprintf to export the cell array by specifying the format of the output data.
I just tried this, and it worked:
filename = 'test';
cell2csv(filename,data)
with cell2csv
available as
function cell2csv(filename,cellArray,delimiter)
% Writes cell array content into a *.csv file.
%
% CELL2CSV(filename,cellArray,delimiter)
%
% filename = Name of the file to save. [ i.e. 'text.csv' ]
% cellarray = Name of the Cell Array where the data is in
% delimiter = seperating sign, normally:',' (default)
%
% by Sylvain Fiedler, KA, 2004
% modified by Rob Kohr, Rutgers, 2005 - changed to english and fixed delimiter
if nargin<3
delimiter = ',';
end
datei = fopen(filename,'w');
for z=1:size(cellArray,1)
for s=1:size(cellArray,2)
var = eval(['cellArray{z,s}']);
if size(var,1) == 0
var = '';
end
if isnumeric(var) == 1
var = num2str(var);
end
fprintf(datei,var);
if s ~= size(cellArray,2)
fprintf(datei,[delimiter]);
end
end
fprintf(datei,'\n');
end
fclose(datei);
I copied the code cause I am not aware if the function is any longer available on MathWorks File Exchange. But Google should help too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With