Skip to content

Commit fef0ddf

Browse files
fix ExportToCSV for double data & added ExportToTSV
1 parent 3de375a commit fef0ddf

File tree

4 files changed

+107
-65
lines changed

4 files changed

+107
-65
lines changed

@BasicObject/ExportToCSV.m

+1-55
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,9 @@
11
function ExportToCSV( self, filename, withHeader )
2-
%EXPORTTOCSV print the self.Header and self.Data in a CSV file, with ';' as separator
3-
% withHeader=1 prints header (default), withHeader=0 does not.
42

53
if nargin < 3
64
withHeader = 1;
75
end
86

9-
10-
%% Open file in write mod
11-
12-
fileID = fopen( [ filename '.csv' ] , 'w' , 'n' , 'UTF-8' );
13-
if fileID < 0
14-
error('%d cannot be opened', filename)
15-
end
16-
17-
18-
%% Fill the file
19-
20-
% Print header
21-
if withHeader
22-
fprintf(fileID, '%s;', self.Header{:});
23-
fprintf(fileID, '\n'); % end of line
24-
end
25-
26-
% Print data
27-
for i = 1 : size(self.Data,1)
28-
for j = 1 : size(self.Data,2)
29-
30-
% Apply conversion if necessary
31-
32-
if ischar(self.Data{i,j})
33-
toprint = self.Data{i,j};
34-
35-
elseif isnumeric(self.Data{i,j})
36-
toprint = num2str(self.Data{i,j});
37-
38-
elseif islogical(self.Data{i,j})
39-
switch self.Data{i,j}
40-
case true
41-
toprint = 'TRUE';
42-
case false
43-
toprint = 'FALSE';
44-
end
45-
46-
end
47-
fprintf(fileID, '%s;', toprint);
48-
49-
% End of line
50-
if j == size(self.Data,2)
51-
fprintf(fileID, '\n');
52-
end
53-
54-
end
55-
end
56-
57-
58-
%% Close the file
59-
60-
fclose( fileID );
61-
7+
self.ExportToTxt(filename,'csv',withHeader)
628

639
end % function

@BasicObject/ExportToTSV.m

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function ExportToTSV( self, filename, withHeader )
2+
3+
if nargin < 3
4+
withHeader = 1;
5+
end
6+
7+
self.ExportToTxt(filename,'tsv',withHeader)
8+
9+
end % function

@BasicObject/ExportToTxt.m

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
function ExportToTxt( self, filename, filetype, withHeader )
2+
%ExportToTxt print the self.Header and self.Data in a text file
3+
% withHeader=1 prints header (default), withHeader=0 does not.
4+
5+
switch filetype
6+
case 'csv'
7+
ext = '.csv';
8+
sep = ';';
9+
case 'tsv'
10+
ext = '.tsv';
11+
sep = sprintf('\t');
12+
otherwise
13+
error('unmapped filetype')
14+
end
15+
16+
17+
%% Open file in write mod
18+
19+
fileID = fopen( [ filename ext ] , 'w' , 'n' , 'UTF-8' );
20+
if fileID < 0
21+
error('%d cannot be opened', filename)
22+
end
23+
24+
25+
%% Fill the file
26+
27+
% Print header
28+
if withHeader
29+
for h = 1 : length(self.Header)
30+
fprintf(fileID, '%s%s', self.Header{h},sep);
31+
end
32+
fprintf(fileID, '\n'); % end of line
33+
end
34+
35+
% Print data
36+
for i = 1 : size(self.Data,1)
37+
for j = 1 : size(self.Data,2)
38+
39+
% Apply conversion if necessary
40+
41+
switch class(self.Data)
42+
43+
case 'cell'
44+
45+
if ischar(self.Data{i,j})
46+
toprint = self.Data{i,j};
47+
48+
elseif isnumeric(self.Data{i,j})
49+
toprint = num2str(self.Data{i,j});
50+
51+
elseif islogical(self.Data{i,j})
52+
switch self.Data{i,j}
53+
case true
54+
toprint = 'TRUE';
55+
case false
56+
toprint = 'FALSE';
57+
end
58+
59+
end
60+
61+
62+
case 'double'
63+
64+
if isnumeric(self.Data(i,j))
65+
toprint = num2str(self.Data(i,j));
66+
67+
elseif islogical(self.Data(i,j))
68+
switch self.Data(i,j)
69+
case true
70+
toprint = 'TRUE';
71+
case false
72+
toprint = 'FALSE';
73+
end
74+
75+
end
76+
77+
otherwise
78+
warning('unmapped input type')
79+
end
80+
81+
fprintf(fileID, '%s%s', toprint,sep);
82+
83+
% End of line
84+
if j == size(self.Data,2)
85+
fprintf(fileID, '\n');
86+
end
87+
88+
end
89+
end
90+
91+
92+
%% Close the file
93+
94+
fclose( fileID );
95+
96+
97+
end % function

StimTemplateContent.m

-10
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,16 @@
88

99
dirContent = dir(path_location);
1010

11-
% dirContent_char = char(dirContent.name);
12-
% ln = size(dirContent_char,2)+1;
13-
% dirContent_category = '';
14-
1511
fprintf(' CONTENT : \n')
1612
for c = 1 : length(dirContent)
1713

1814
if dirContent(c).isdir
19-
% dirContent_category(c,1:4) = 'dir ';
20-
% dirContent_char(c,ln) = filesep;
2115
fprintf('%s%s\n',dirContent(c).name,filesep)
22-
2316
else
24-
% dirContent_category(c,1:4) = 'file';
25-
% dirContent_char(c,ln) = ' ';
2617
fprintf('%s\n',dirContent(c).name)
2718

2819
end
2920

3021
end
31-
% disp(dirContent_char)
3222

3323
end % function

0 commit comments

Comments
 (0)