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