forked from LIMO-EEG-Toolbox/limo_tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cell2csv.m
85 lines (76 loc) · 2.33 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
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. [ i.e. 'text.csv' ]
% cellArray = Name of the Cell Array where the data is in
% 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 (;)
% decimal = defines the decimal separator (default = '.')
%
% by Sylvain Fiedler, KA, 2004
% updated by Sylvain Fiedler, Metz, 06
% fixed the logical-bug, Kaiserslautern, 06/2008, S.Fiedler
% added the choice of decimal separator, 11/2010, S.Fiedler
%% Checking für 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
%% Write file
datei = fopen(fileName, 'w');
for z=1:size(cellArray, 1)
for s=1:size(cellArray, 2)
var = eval(['cellArray{z,s}']);
% If zero, then empty cell
if size(var, 1) == 0
var = '';
end
% If numeric -> String
if isnumeric(var)
var = num2str(var);
% Conversion of decimal separator (4 Europe & South America)
% http://commons.wikimedia.org/wiki/File:DecimalSeparator.svg
if decimal ~= '.'
var = strrep(var, '.', decimal);
end
end
% If logical -> 'true' or 'false'
if islogical(var)
if var == 1
var = 'TRUE';
else
var = 'FALSE';
end
end
% If newer version of Excel -> Quotes 4 Strings
if excelYear > 2000
var = ['"' var '"'];
end
% OUTPUT value
fprintf(datei, '%s', var);
% OUTPUT separator
if s ~= size(cellArray, 2)
fprintf(datei, separator);
end
end
if z ~= size(cellArray, 1) % prevent a empty line at EOF
% OUTPUT newline
fprintf(datei, '\n');
end
end
% Closing file
fclose(datei);
% END