generate/cell2csv.m
function cell2csv(fileName, cellArray, append, separator, excelYear, decimal)
% Writes cell array content into a *.csv file.
%
% CELL2CSV(fileName, cellArray, append, separator, excelYear, decimal)
%
% fileName = Name of the file to save. [ i.e. 'text.csv' ]
% cellArray = Name of the Cell Array where the data is in
% append = false to open a new file, or true to go on with the existent.
% separator = sign separating the values (default = ';')
% excelYear = depending on the Excel version, the cells are put into
% quotes before they are written to the file. The separator
% is set to semicolon (;)
% decimal = defines the decimal separator (default = '.')
%
% by Sylvain Fiedler, KA, 2004
% updated by Sylvain Fiedler, Metz, 06
% fixed the logical-bug, Kaiserslautern, 06/2008, S.Fiedler
% added the choice of decimal separator, 11/2010, S.Fiedler
%% Checking for optional Variables
if ~exist('append', 'var')
append = false;
end
if ~exist('separator', 'var')
separator = ',';
end
if ~exist('excelYear', 'var')
excelYear = 1997;
end
if ~exist('decimal', 'var')
decimal = '.';
end
%% Setting separator for newer excelYears
if excelYear > 2000
separator = ';';
end
%% Decide if we delete or we append
if ~append||~exist(fileName,'file');
datei = fopen(fileName, 'w');
else
datei = fopen(fileName, 'a+');
fprintf(datei, '\n');
end
%% Write file
for z=1:size(cellArray, 1)
for s=1:size(cellArray, 2)
var = eval('cellArray{z,s}');
% If zero, then empty cell
if size(var, 1) == 0
var = '';
end
% If numeric -> String
if isnumeric(var)
var = num2str(var);
% Conversion of decimal separator (4 Europe & South America)
% http://commons.wikimedia.org/wiki/File:DecimalSeparator.svg
if decimal ~= '.'
var = strrep(var, '.', decimal);
end
end
% If logical -> 'true' or 'false'
if islogical(var)
if var == 1
var = 'TRUE';
else
var = 'FALSE';
end
end
% If newer version of Excel -> Quotes 4 Strings
if excelYear > 2000
var = ['"' var '"'];
end
% OUTPUT value
fprintf(datei, '%s', var);
% OUTPUT separator
if s ~= size(cellArray, 2)
fprintf(datei, separator);
end
end
if z ~= size(cellArray, 1) % prevent a empty line at EOF
% OUTPUT newline
fprintf(datei, '\n');
end
end
% Closing file
fclose(datei);
% END