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