|
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. [ e.g. 'text.csv' ] |
7 |
| -% cellArray = Name of the Cell Array where the data is in |
8 |
| -% |
9 |
| -% optional: |
10 |
| -% separator = sign separating the values (default = ',') |
11 |
| -% excelYear = depending on the Excel version, the cells are put into |
12 |
| -% quotes before they are written to the file. The separator |
13 |
| -% is set to semicolon (;) (default = 1997 which does not change separator to semicolon ;) |
14 |
| -% decimal = defines the decimal separator (default = '.') |
15 |
| -% |
16 |
| -% by Sylvain Fiedler, KA, 2004 |
17 |
| -% updated by Sylvain Fiedler, Metz, 06 |
18 |
| -% updated by Philipp Schneider, 2019 |
19 |
| -% fixed the logical-bug, Kaiserslautern, 06/2008, S.Fiedler |
20 |
| -% added the choice of decimal separator, 11/2010, S.Fiedler |
21 |
| -% modfiedy and optimized by Jerry Zhu, June, 2014, [email protected] |
22 |
| -% now works with empty cells, numeric, char, string, row vector, and logical cells. |
23 |
| -% row vector such as [1 2 3] will be separated by two spaces, that is "1 2 3" |
24 |
| -% One array can contain all of them, but only one value per cell. |
25 |
| -% 2x times faster than Sylvain's codes (8.8s vs. 17.2s): |
26 |
| -% tic;C={'te','tm';5,[1,2];true,{}};C=repmat(C,[10000,1]);cell2csv([datestr(now,'MMSS') '.csv'],C);toc; |
27 |
| - |
28 |
| -%% Checking for optional Variables |
29 |
| -if ~exist('separator', 'var') |
30 |
| - separator = ','; |
31 |
| -end |
32 |
| - |
33 |
| -if ~exist('excelYear', 'var') |
34 |
| - excelYear = 1997; |
35 |
| -end |
36 |
| - |
37 |
| -if ~exist('decimal', 'var') |
38 |
| - decimal = '.'; |
39 |
| -end |
40 |
| - |
41 |
| -%% Setting separator for newer excelYears |
42 |
| -if excelYear > 2000 |
43 |
| - separator = ';'; |
44 |
| -end |
45 |
| - |
46 |
| -% convert cell |
47 |
| -cellArray = cellfun(@StringX, cellArray, 'UniformOutput', false); |
48 |
| -cellArray = regexprep(cellArray,'(?<!%)%(?!%)','%%'); % to replace escape characters |
49 |
| - |
50 |
| -%% Write file |
51 |
| -datei = fopen(fileName, 'w'); |
52 |
| -[nrows,ncols] = size(cellArray); |
53 |
| -for row = 1:nrows |
54 |
| - fprintf(datei,[sprintf(['%s' separator],cellArray{row,1:ncols-1}) cellArray{row,ncols} '\n']); |
55 |
| -end |
56 |
| -% Closing file |
57 |
| -fclose(datei); |
58 |
| - |
59 |
| -% sub-function |
60 |
| -function x = StringX(x) |
61 |
| - % If ischar, do nothing |
62 |
| - if ischar(x) |
63 |
| - % If zero, then empty cell |
64 |
| - elseif isempty(x) |
65 |
| - x = ''; |
66 |
| - % If numeric -> String, e.g. 1, [1 2] |
67 |
| - elseif isnumeric(x) && isrow(x) |
68 |
| - x = num2str(x); |
69 |
| - if decimal ~= '.' |
70 |
| - x = strrep(x, '.', decimal); |
71 |
| - end |
72 |
| - % If logical -> 'true' or 'false' |
73 |
| - elseif islogical(x) |
74 |
| - if x == 1 |
75 |
| - x = 'TRUE'; |
76 |
| - else |
77 |
| - x = 'FALSE'; |
78 |
| - end |
79 |
| - % If matrix array -> a1 a2 a3. e.g. [1 2 3] |
80 |
| - % also catch string or char here |
81 |
| - elseif isrow(x) && ~iscell(x) |
82 |
| - x = num2str(x); |
83 |
| - % everthing else, such as [1;2], {1} |
84 |
| - else |
85 |
| - x = 'NA'; |
86 |
| - end |
87 |
| - |
88 |
| - % If newer version of Excel -> Quotes 4 Strings |
89 |
| - if excelYear > 2000 |
90 |
| - x = ['"' x '"']; |
91 |
| - end |
92 |
| -end % end sub-function |
93 |
| -end % end function |
| 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. [ e.g. 'text.csv' ] |
| 7 | +% cellArray = Name of the Cell Array where the data is in |
| 8 | +% |
| 9 | +% optional: |
| 10 | +% separator = sign separating the values (default = ',') |
| 11 | +% excelYear = depending on the Excel version, the cells are put into |
| 12 | +% quotes before they are written to the file. The separator |
| 13 | +% is set to semicolon (;) (default = 1997 which does not change separator to semicolon ;) |
| 14 | +% decimal = defines the decimal separator (default = '.') |
| 15 | +% |
| 16 | +% by Sylvain Fiedler, KA, 2004 |
| 17 | +% updated by Sylvain Fiedler, Metz, 06 |
| 18 | +% updated by Philipp Schneider, 2019 |
| 19 | +% fixed the logical-bug, Kaiserslautern, 06/2008, S.Fiedler |
| 20 | +% added the choice of decimal separator, 11/2010, S.Fiedler |
| 21 | +% modfiedy and optimized by Jerry Zhu, June, 2014, [email protected] |
| 22 | +% now works with empty cells, numeric, char, string, row vector, and logical cells. |
| 23 | +% row vector such as [1 2 3] will be separated by two spaces, that is "1 2 3" |
| 24 | +% One array can contain all of them, but only one value per cell. |
| 25 | +% 2x times faster than Sylvain's codes (8.8s vs. 17.2s): |
| 26 | +% tic;C={'te','tm';5,[1,2];true,{}};C=repmat(C,[10000,1]);cell2csv([datestr(now,'MMSS') '.csv'],C);toc; |
| 27 | + |
| 28 | +%% Checking for optional Variables |
| 29 | +if ~exist('separator', 'var') |
| 30 | + separator = ','; |
| 31 | +end |
| 32 | + |
| 33 | +if ~exist('excelYear', 'var') |
| 34 | + excelYear = 1997; |
| 35 | +end |
| 36 | + |
| 37 | +if ~exist('decimal', 'var') |
| 38 | + decimal = '.'; |
| 39 | +end |
| 40 | + |
| 41 | +%% Setting separator for newer excelYears |
| 42 | +if excelYear > 2000 |
| 43 | + separator = ';'; |
| 44 | +end |
| 45 | + |
| 46 | +% convert cell |
| 47 | +cellArray = cellfun(@StringX, cellArray, 'UniformOutput', false); |
| 48 | +cellArray = regexprep(cellArray,'(?<!%)%(?!%)','%%'); % to replace escape characters |
| 49 | + |
| 50 | +%% Write file |
| 51 | +datei = fopen(fileName, 'w'); |
| 52 | +[nrows,ncols] = size(cellArray); |
| 53 | +for row = 1:nrows |
| 54 | + fprintf(datei,[sprintf(['%s' separator],cellArray{row,1:ncols-1}) cellArray{row,ncols} '\n']); |
| 55 | +end |
| 56 | +% Closing file |
| 57 | +fclose(datei); |
| 58 | + |
| 59 | +% sub-function |
| 60 | +function x = StringX(x) |
| 61 | + % If ischar, do nothing |
| 62 | + if ischar(x) |
| 63 | + % If zero, then empty cell |
| 64 | + elseif isempty(x) |
| 65 | + x = ''; |
| 66 | + % If numeric -> String, e.g. 1, [1 2] |
| 67 | + elseif isnumeric(x) && isrow(x) |
| 68 | + x = num2str(x); |
| 69 | + if decimal ~= '.' |
| 70 | + x = strrep(x, '.', decimal); |
| 71 | + end |
| 72 | + % If logical -> 'true' or 'false' |
| 73 | + elseif islogical(x) |
| 74 | + if x == 1 |
| 75 | + x = 'TRUE'; |
| 76 | + else |
| 77 | + x = 'FALSE'; |
| 78 | + end |
| 79 | + % If matrix array -> a1 a2 a3. e.g. [1 2 3] |
| 80 | + % also catch string or char here |
| 81 | + elseif isrow(x) && ~iscell(x) |
| 82 | + x = num2str(x); |
| 83 | + % everthing else, such as [1;2], {1} |
| 84 | + else |
| 85 | + x = 'NA'; |
| 86 | + end |
| 87 | + |
| 88 | + % If newer version of Excel -> Quotes 4 Strings |
| 89 | + if excelYear > 2000 |
| 90 | + x = ['"' x '"']; |
| 91 | + end |
| 92 | +end % end sub-function |
| 93 | +end % end function |
0 commit comments