graph.m 4.73 KB
Newer Older
Houtan Bastani's avatar
Houtan Bastani committed
1
2
3
4
5
function o = graph(varargin)
%function o = graph(varargin)
% Graph Class Constructor
%
% INPUTS
Houtan Bastani's avatar
Houtan Bastani committed
6
7
8
9
%   varargin        0 args  : empty graph object
%                   1 arg   : must be graph object (return a copy of arg)
%                   > 1 args: option/value pairs (see structure below for
%                   options)
Houtan Bastani's avatar
Houtan Bastani committed
10
11
%
% OUTPUTS
Houtan Bastani's avatar
Houtan Bastani committed
12
%   o   [graph] graph object
Houtan Bastani's avatar
Houtan Bastani committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
%
% SPECIAL REQUIREMENTS
%   none

% Copyright (C) 2013 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.

o = struct;
Houtan Bastani's avatar
Houtan Bastani committed
35
36
37
38
39
40

o.config = '';

o.title = '';
o.ylabel = '';
o.xlabel = '';
Houtan Bastani's avatar
Houtan Bastani committed
41
o.footnote = '';
Houtan Bastani's avatar
Houtan Bastani committed
42
43

o.figname = '';
Houtan Bastani's avatar
Houtan Bastani committed
44
o.data = '';
45
o.seriestouse = '';
Houtan Bastani's avatar
Houtan Bastani committed
46
o.shade = '';
47
48
o.xrange = '';
o.yrange = '';
Houtan Bastani's avatar
Houtan Bastani committed
49

Houtan Bastani's avatar
Houtan Bastani committed
50
51
o.grid = true;

Houtan Bastani's avatar
Houtan Bastani committed
52
53
54
55
o.legend = false;
o.legend_location = 'SouthEast';
o.legend_orientation = 'horizontal';
o.legend_font_size = 8;
Houtan Bastani's avatar
Houtan Bastani committed
56
57

if nargin == 1
58
59
    assert(isa(varargin{1}, 'graph'),['@graph.graph: with one arg you ' ...
                        'must pass a graph object']);
Houtan Bastani's avatar
Houtan Bastani committed
60
61
62
63
    o = varargin{1};
    return;
elseif nargin > 1
    if round(nargin/2) ~= nargin/2
64
        error(['@graph.graph: options must be supplied in name/value ' ...
Houtan Bastani's avatar
Houtan Bastani committed
65
66
67
68
69
70
71
72
73
74
75
               'pairs.']);
    end

    optNames = lower(fieldnames(o));

    % overwrite default values
    for pair = reshape(varargin, 2, [])
        field = lower(pair{1});
        if any(strmatch(field, optNames, 'exact'))
            o.(field) = pair{2};
        else
76
            error('@graph.graph: %s is not a recognized option.', field);
Houtan Bastani's avatar
Houtan Bastani committed
77
78
79
80
        end
    end
end

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
% Check options provided by user
assert(ischar(o.title), '@graph.graph: title must be a string');
assert(ischar(o.footnote), '@graph.graph: footnote must be a string');
assert(ischar(o.config), '@graph.graph: config file must be a string');
assert(ischar(o.xlabel), '@graph.graph: xlabel file must be a string');
assert(ischar(o.ylabel), '@graph.graph: ylabel file must be a string');
assert(ischar(o.figname), '@graph.graph: figname must be a string');
assert(islogical(o.grid), '@graph.graph: grid must be either true or false');
assert(islogical(o.legend), '@graph.graph: legend must be either true or false');
assert(isint(o.legend_font_size), '@graph.graph: legend_font_size must be an integer');
valid_legend_locations = ...
    {'North', 'South', 'East', 'West', ...
     'NorthEast', 'SouthEast', 'NorthWest', 'SouthWest', ...
     'NorthOutside', 'SouthOutside', 'EastOutside', 'WestOutside', ...
     'NorthEastOutside', 'SouthEastOutside', 'NorthWestOutside', 'SouthWestOutside', ...
     'Best', 'BestOutside', ...
    };
assert(any(strcmp(o.legend_location, valid_legend_locations)), ...
       ['@graph.graph: legend_location must be one of ' strjoin(valid_legend_locations, ' ')]);

valid_legend_orientations = {'vertical', 'horizontal'};
assert(any(strcmp(o.legend_orientation, valid_legend_orientations)), ...
       ['@graph.graph: legend_orientation must be one of ' strjoin(valid_legend_orientations, ' ')]);

105
106
107
108
109
110
111
112
113
114
assert(isempty(o.shade) || (isa(o.shade, 'dynDates') && o.shade.ndat >= 2), ...
       ['@graph.graph: shade is specified as a dynDates range, e.g. ' ...
        '''dynDates(''1999q1''):dynDates(''1999q3'')''.']);
assert(isempty(o.xrange) || (isa(o.xrange, 'dynDates') && o.xrange.ndat >= 2), ...
       ['@graph.graph: xrange is specified as a dynDates range, e.g. ' ...
        '''dynDates(''1999q1''):dynDates(''1999q3'')''.']);
assert(isempty(o.yrange) || (isfloat(o.yrange) && length(o.yrange) == 2 && ...
                             o.yrange(1) < o.yrange(2)), ...
       ['@graph.graph: yrange is specified an array with two float entries, ' ...
        'the lower bound and upper bound.']);
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

assert(~isempty(o.data), '@graph.graph: must provide data');
msg = ['@graph.graph: data must either be a dynSeries or a cell array of ' ...
       'dynSeries'];
if length(o.data) == 1
    assert(isa(o.data, 'dynSeries'), msg);
else
    assert(iscell(o.data), msg);
    for i=1:length(o.data)
        assert(isa(o.data{i}, 'dynSeries'), msg);
    end
end

msg = ['@graph.graph: series to use must be a cell array of string(s)'];
if ~isempty(o.seriestouse)
    assert(iscell(o.seriestouse), msg);
    for i=1:length(o.seriestouse)
        assert(ischar(o.seriestouse{i}), msg);
    end
end

Houtan Bastani's avatar
Houtan Bastani committed
136
137
138
% Create graph object
o = class(o, 'graph');
end