1function rplot(s1)
2% function rplot(s1)
3%
4% Plots the simulated trajectory of one or several variables.
5% The entire simulation period is plotted, unless instructed otherwise
6% with "dsample".
7%
8% INPUTS
9%    s1    [cell]           variable names
10%
11% OUTPUTS
12%    none
13%
14% SPECIAL REQUIREMENTS
15%    none
16
17% Copyright (C) 2001-2019 Dynare Team
18%
19% This file is part of Dynare.
20%
21% Dynare is free software: you can redistribute it and/or modify
22% it under the terms of the GNU General Public License as published by
23% the Free Software Foundation, either version 3 of the License, or
24% (at your option) any later version.
25%
26% Dynare is distributed in the hope that it will be useful,
27% but WITHOUT ANY WARRANTY; without even the implied warranty of
28% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29% GNU General Public License for more details.
30%
31% You should have received a copy of the GNU General Public License
32% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
33
34global M_ oo_ options_
35
36if isempty(oo_.endo_simul)
37    error('rplot: oo_.endo_simul is empty.')
38end
39
40% create subdirectory <fname>/graphs if it doesn't exist
41if ~exist(M_.fname, 'dir')
42    mkdir('.',M_.fname);
43end
44if ~exist([M_.fname filesep 'graphs'],'dir')
45    mkdir(M_.fname,'graphs');
46end
47
48ix = (1 - M_.maximum_lag:size(oo_.endo_simul,2)-M_.maximum_lag)';
49
50y = [];
51for k = 1:length(s1)
52    if ~any(strcmp(s1{k}, M_.endo_names))
53        if ~any(strcmp(s1{k}, M_.exo_names))
54            error ('rplot: One of the variables specified does not exist') ;
55        else
56            y = [y; oo_.exo_simul(:, strcmp(s1{k}, M_.exo_names))'] ;
57        end
58    else
59        y = [y; oo_.endo_simul(strcmp(s1{k}, M_.endo_names), :)];
60    end
61end
62
63if options_.smpl == 0
64    i = (max(1, M_.maximum_lag):size(oo_.endo_simul,2))';
65else
66    i = (options_.smpl(1)+M_.maximum_lag:options_.smpl(2)+M_.maximum_lag)';
67end
68
69if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format)))
70    fidTeX = fopen([M_.fname, filesep, 'graphs', filesep, M_.fname '_simulated_trajectories_', num2str(options_.rplottype), '.tex'],'w');
71    fprintf(fidTeX,'%% TeX eps-loader file generated by rplot.m (Dynare).\n');
72    fprintf(fidTeX,['%% ' datestr(now,0) '\n']);
73end
74
75if options_.rplottype == 0
76    hh=dyn_figure(options_.nodisplay,'Name', 'Simulated Trajectory');
77    plot(ix(i),y(:,i)) ;
78    title (['Plot of ' strjoin(s1, ' ')],'Interpreter','none') ;
79    xlabel('Periods') ;
80    xlim([min(ix(i)) max(ix(i))])
81    if length(s1) > 1
82        if isoctave
83            legend(s1);
84        else
85            h = legend(s1);
86            set(h, 'Interpreter', 'none');
87        end
88    end
89    dyn_saveas(hh,[M_.fname, filesep, 'graphs', filesep, 'SimulatedTrajectory_' s1{1}],options_.nodisplay,options_.graph_format)
90    if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format)))
91        create_TeX_loader(fidTeX,[M_.fname, '/graphs/', 'SimulatedTrajectory_' s1{1}],'Simulated trajectories','SimulatedTrajectory_',s1{1},1)
92    end
93elseif options_.rplottype == 1
94    for j = 1:size(y,1)
95        hh=dyn_figure(options_.nodisplay,'Name', 'Simulated Trajectory');
96        plot(ix(i),y(j,i)) ;
97        xlim([min(ix(i)) max(ix(i))])
98        title(['Plot of ' s1{j}],'Interpreter','none') ;
99        xlabel('Periods') ;
100        dyn_saveas(hh,[M_.fname, filesep, 'graphs', filesep, 'SimulatedTrajectory_' s1{j}],options_.nodisplay,options_.graph_format)
101        if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format)))
102            create_TeX_loader(fidTeX,[M_.fname, '/graphs/', 'SimulatedTrajectory_' s1{j}],'Simulated trajectories','SimulatedTrajectory_',s1{j},1);
103        end
104    end
105elseif options_.rplottype == 2
106    hh=dyn_figure(options_.nodisplay,'Name', 'Simulated Trajectory');
107    nl = max(1,fix(size(y,1)/4)) ;
108    nc = ceil(size(y,1)/nl) ;
109    for j = 1:size(y,1)
110        subplot(nl,nc,j) ;
111        plot(ix(i),y(j,i)) ;
112        hold on ;
113        if any(strcmp(s1{j}, M_.endo_names))
114            plot(ix(i),oo_.steady_state(strcmp(s1{j}, M_.endo_names))*ones(1,size(i,1)),'r:') ;
115        else
116            plot(ix(i),oo_.exo_steady_state(strcmp(s1{j}, M_.exo_names))*ones(1,size(i,1)),'r:') ;
117        end
118        xlabel('Periods') ;
119        ylabel([s1{j}],'Interpreter','none') ;
120        title(['Plot of ' s1{j}],'Interpreter','none') ;
121        axis tight;
122    end
123    dyn_saveas(hh,[M_.fname, filesep, 'graphs', filesep, 'SimulatedTrajectory_' s1{1}],options_.nodisplay,options_.graph_format)
124    if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format)))
125        create_TeX_loader(fidTeX,[M_.fname, '/graphs/', 'SimulatedTrajectory_' s1{1}],'Simulated trajectories','SimulatedTrajectory_', s1{1},min(j/nc,1));
126    end
127end
128
129if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format)))
130    fprintf(fidTeX,'%% End Of TeX file. \n');
131    fclose(fidTeX);
132end
133end
134
135function create_TeX_loader(fidTeX,figpath,caption,label_name,label_type,scale_factor)
136if nargin<5
137    scale_factor=1;
138end
139fprintf(fidTeX,' \n');
140fprintf(fidTeX,'\\begin{figure}[H]\n');
141fprintf(fidTeX,'\\centering \n');
142fprintf(fidTeX,'\\includegraphics[width=%2.2f\\textwidth]{%s}\n',0.8*scale_factor,strrep(figpath,'\','/'));
143fprintf(fidTeX,'\\caption{%s.}',caption);
144fprintf(fidTeX,'\\label{Fig:%s:%s}\n',label_name,label_type);
145fprintf(fidTeX,'\\end{figure}\n\n');
146end