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