forked from brian-lau/MatlabStan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RData.m
91 lines (85 loc) · 2.84 KB
/
RData.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
86
87
88
89
90
91
classdef RData < handle
%RData
% usage
% d = RData( map_or_struct );
% d.value('N') = 15; % create or over-write value of 'N'
% d.type('N') = 'matrix'; % override auto-typed value of 'scalar' with a 1x1 matrix
% d.rdump(file) %
%
% Mike Boedigheimer
properties
value;
type;
end
methods
function a = RData( map_or_struct )
a.value = map_or_struct;
end
function set.value(a,dat)
if isempty(dat)
a.value = containers.Map('KeyType', 'char', 'KeyValue', 'any');
a.type = containers.Map('KeyType', 'char', 'KeyValue', 'char');
return;
end
if isa(dat, 'struct')
names = fieldnames(dat);
values = struct2cell(dat);
a.value = containers.Map( names, values );
a.init_data_type();
elseif isa(dat, 'containers.Map')
a.value = dat;
a.init_data_type();
end
end
function rdump(a,fname)
try
fid = fopen(fname,'wt');
vars = a.value.keys;
data = a.value.values;
types = a.type.values;
for i = 1:length(vars)
if isempty(data{i})
continue;
end
if any(data{i}(:) > intmax('int32'))
ts = textscan( sprintf( '%f\n', data{i}(:) ), '%s', 'delimiter', '\n' );
else
ts = textscan( sprintf( '%d\n', data{i}(:) ), '%s', 'delimiter', '\n' );
end
num_list = strjoin( ts{1}, ',');
switch( types{i} )
case 'scalar'
fprintf(fid,'%s <- %s\n',vars{i},num_list);
case 'vector'
fprintf(fid,'%s <- c(%s)\n',vars{i},num_list);
case 'matrix'
fprintf(fid,'%s <- structure(c(%s), .Dim=c(%g, %g))\n', vars{i}, num_list, size(data{i},1), size(data{i},2));
end
end
fclose(fid);
catch
if strcmp(ME.identifier, 'MATLAB:FileIO:InvalidFid')
disp('Could not find the specified file.');
rethrow(ME);
end
end
end
end
methods( Access=protected)
function vartype = init_data_type( a )
x = a.value.values;
n = length(x);
vartype = repmat({''}, n,1);
for i = 1:n
if isscalar(x{i})
vartype{i}= 'scalar';
elseif isvector(x{i})
vartype{i} = 'vector';
elseif ismatrix(x{i})
vartype{i} = 'matrix';
end
end
a.type = containers.Map( a.value.keys, vartype );
end
end
end