comparison core/tools/cell2csv.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function cell2csv(fileName, cellArray, separator, excelYear, decimal)
2 % Writes cell array content into a *.csv file.
3 %
4 % CELL2CSV(fileName, cellArray, separator, excelYear, decimal)
5 %
6 % fileName = Name of the file to save. [ i.e. 'text.csv' ]
7 % cellArray = Name of the Cell Array where the data is in
8 % separator = sign separating the values (default = ';')
9 % excelYear = depending on the Excel version, the cells are put into
10 % quotes before they are written to the file. The separator
11 % is set to semicolon (;)
12 % decimal = defines the decimal separator (default = '.')
13 %
14 % by Sylvain Fiedler, KA, 2004
15 % updated by Sylvain Fiedler, Metz, 06
16 % fixed the logical-bug, Kaiserslautern, 06/2008, S.Fiedler
17 % added the choice of decimal separator, 11/2010, S.Fiedler
18
19 %% Checking für optional Variables
20 if ~exist('separator', 'var')
21 separator = ',';
22 end
23
24 if ~exist('excelYear', 'var')
25 excelYear = 1997;
26 end
27
28 if ~exist('decimal', 'var')
29 decimal = '.';
30 end
31
32 %% Setting separator for newer excelYears
33 if excelYear > 2000
34 separator = ';';
35 end
36
37 %% Write file
38 datei = fopen(fileName, 'w');
39
40 for z=1:size(cellArray, 1)
41 for s=1:size(cellArray, 2)
42
43 var = eval(['cellArray{z,s}']);
44 % If zero, then empty cell
45 if size(var, 1) == 0
46 var = '';
47 end
48 % If numeric -> String
49 if isnumeric(var)
50 var = num2str(var);
51 % Conversion of decimal separator (4 Europe & South America)
52 % http://commons.wikimedia.org/wiki/File:DecimalSeparator.svg
53 if decimal ~= '.'
54 var = strrep(var, '.', decimal);
55 end
56 end
57 % If logical -> 'true' or 'false'
58 if islogical(var)
59 if var == 1
60 var = 'TRUE';
61 else
62 var = 'FALSE';
63 end
64 end
65 % If newer version of Excel -> Quotes 4 Strings
66 if excelYear > 2000
67 var = ['"' var '"'];
68 end
69
70 % OUTPUT value
71 fprintf(datei, '%s', var);
72
73 % OUTPUT separator
74 if s ~= size(cellArray, 2)
75 fprintf(datei, separator);
76 end
77 end
78 if z ~= size(cellArray, 1) % prevent a empty line at EOF
79 % OUTPUT newline
80 fprintf(datei, '\n');
81 end
82 end
83 % Closing file
84 fclose(datei);
85 % END