forked from omanor/PIPredictor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriteMatrixWithHeader2TabDelimited.m
More file actions
60 lines (49 loc) · 1.46 KB
/
WriteMatrixWithHeader2TabDelimited.m
File metadata and controls
60 lines (49 loc) · 1.46 KB
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
function WriteMatrixWithHeader2TabDelimited(data, col_headers, row_headers, out_file_name, print_nans)
%function WriteMatrixWithHeader2TabDelimited(data, col_headers, row_headers, out_file_name, print_nans)
if ~exist('print_nans','var')
print_nans = 0;
end
if length(col_headers) - 1 ~= size(data,2) && ~isempty(col_headers)
error('col headers number do not fit to data: ' );
end
if length(row_headers) ~= size(data,1)
error('row headers number do not fit to data');
end
fid = fopen(out_file_name, 'w');
if fid < 0
error(['File ' out_file_name ' was not found.']);
end
if ~isempty(col_headers)
for i=1:length(col_headers)
if (i > 1)
fprintf(fid,'%s', char(9));
end
if isnumeric(col_headers)
fprintf(fid,'%s', num2str(col_headers(i)));
else
fprintf(fid,'%s', col_headers{i});
end
end
fprintf(fid,'\n');
end
r_num = size(data,1);
c_num = size(data,2);
for r=1:r_num
if isnumeric(row_headers)
fprintf(fid,'%s', num2str(row_headers(r)));
else
fprintf(fid,'%s', row_headers{r});
end
for c=1:c_num
fprintf(fid,'%s', char(9));
if print_nans ~= 0 || ~isnan(data(r,c))
if ((data(r,c) ~= 0) && (abs(data(r,c)) < 0.000001))
fprintf(fid,'%e', data(r,c));
else
fprintf(fid,'%f', data(r,c));
end
end
end
fprintf(fid,'\n');
end
fclose(fid);