forked from saifuddin-m/DAMM-MCNiP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csvwrite_with_headers.m
72 lines (62 loc) · 1.8 KB
/
csvwrite_with_headers.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
% This function functions like the build in MATLAB function csvwrite but
% allows a row of headers to be easily inserted
%
% known limitations
% The same limitation that apply to the data structure that exist with
% csvwrite apply in this function, notably:
% m must not be a cell array
%
% Inputs
%
% filename - Output filename
% m - array of data
% headers - a cell array of strings containing the column headers.
% The length must be the same as the number of columns in m.
% r - row offset of the data (optional parameter)
% c - column offset of the data (optional parameter)
%
%
% Outputs
% None
function csvwrite_with_headers(filename,m,headers,r,c)
%% initial checks on the inputs
if ~ischar(filename)
error('FILENAME must be a string');
end
% the r and c inputs are optional and need to be filled in if they are
% missing
if nargin < 4
r = 0;
end
if nargin < 5
c = 0;
end
if ~iscellstr(headers)
error('Header must be cell array of strings')
end
if length(headers) ~= size(m,2)
error('number of header entries must match the number of columns in the data')
end
%% write the header string to the file
%turn the headers into a single comma seperated string if it is a cell
%array,
header_string = headers{1};
for i = 2:length(headers)
header_string = [header_string,',',headers{i}];
end
%if the data has an offset shifting it right then blank commas must
%be inserted to match
if r>0
for i=1:r
header_string = [',',header_string];
end
end
%write the string to a file
fid = fopen(filename,'w');
fprintf(fid,'%s\r\n',header_string);
fclose(fid);
%% write the append the data to the file
%
% Call dlmwrite with a comma as the delimiter
%
dlmwrite(filename, m,'-append','delimiter',',','roffset', r,'coffset',c);