-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite_dataset.m
More file actions
66 lines (53 loc) · 2.07 KB
/
write_dataset.m
File metadata and controls
66 lines (53 loc) · 2.07 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
61
62
63
64
65
function write_dataset(filename, data, colnames, precisions)
%WRITE_DATASET Write Moodle question dataset to file.
% Write the dataset contained in matrix data to a tab-delimited file
% (each row in data corresponds to one question, and each column
% corresponds to a specific wildcard).
%
% Arguments:
% filename -- overwrite this file with the given dataset
% data -- rows of questions by cols of wildcards
% colnames -- name of wildcard for each column, EXCLUDING braces
% precisions -- number of digits after the decimal point for each col
[data_nr, data_nc] = size(data);
[names_nr, names_nc] = size(colnames);
if names_nr > names_nc
colnames = transpose(colnames);
end
[names_nr, names_nc] = size(colnames);
[precisions_nr, precisions_nc] = size(precisions);
if precisions_nr > precisions_nc
precisions = transpose(precisions);
end
[precisions_nr, precisions_nc] = size(precisions);
if data_nc ~= names_nc
error('UWMadison:MoodleDataset:colNameMismatch', ...
'Number of data columns and column names do not match', ...
data_nc, ...
names_nc)
end
if data_nc ~= precisions_nc
error('UWMadison:MoodleDataset:colPrecisionMismatch', ...
'Number of data columns and column precisions do not match', ...
data_nc, ...
precisions_nc)
end
fid = fopen(filename, 'w');
%%% Write column names to file.
namestr = sprintf('%s\t', colnames{:});
fprintf(fid, '%s\n', strtrim(namestr));
% Prepare row-wise fprintf format string.
formatstr = sprintf('%%0.%df\\t', precisions);
% Remove ending '\t'.
assert(all(formatstr(length(formatstr)-1 : length(formatstr)) == '\t'))
formatstr(length(formatstr)-1 : length(formatstr)) = '';
% Append ending '\n'.
formatstr = strcat(formatstr, '\n');
%%% Write data to file.
for row = 1:data_nr
fprintf(fid, formatstr, data(row,:));
end
fclose(fid);
fprintf('Wrote %s with %d rows and %d cols\n', ...
filename, data_nr, data_nc)
end