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