1function check_matlab_path(change_path_flag) 2 3% Copyright (C) 2015-2017 Dynare Team 4% 5% This file is part of Dynare. 6% 7% Dynare is free software: you can redistribute it and/or modify 8% it under the terms of the GNU General Public License as published by 9% the Free Software Foundation, either version 3 of the License, or 10% (at your option) any later version. 11% 12% Dynare is distributed in the hope that it will be useful, 13% but WITHOUT ANY WARRANTY; without even the implied warranty of 14% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15% GNU General Public License for more details. 16% 17% You should have received a copy of the GNU General Public License 18% along with Dynare. If not, see <http://www.gnu.org/licenses/>. 19 20if ~nargin || isempty(change_path_flag) 21 change_path_flag = true; 22end 23 24% Get path to dynare/matlab folder. 25DYNARE_PATH = strrep(which('dynare'),'dynare.m',''); 26 27if isempty(DYNARE_PATH) 28 % Nothing to do here (this case should not happen) 29 disp('dynare.m is not in the Matlab''s path.') 30 return 31else 32 % Removes trailing slash. 33 DYNARE_PATH = DYNARE_PATH(1:end-1); 34end 35 36% Get matlab path 37MATLAB_PATH = path(); 38if isoctave 39 % Octave always has '.' at the top of the path, so remove '.:' 40 MATLAB_PATH = MATLAB_PATH(3:end); 41end 42 43% Position of DYNARE_PATH in MATLAB_PATH 44idDYNARE = strfind(MATLAB_PATH,DYNARE_PATH); 45 46if isempty(idDYNARE) 47 disp('dynare.m is not in the Matlab''s path.') 48 return 49else 50 if isequal(length(idDYNARE),1) 51 if isequal(idDYNARE, 1) 52 % Dynare is on top of matlab's path! Nothing to do here... 53 return 54 else 55 str0 = sprintf('Dynare is not on top of the Matlab/Octave path!'); 56 % Check that this will not create a problem 57 MATLAB_PATH_ = path2cell(MATLAB_PATH); 58 DYNARE_ROUTINES = getallroutinenames(DYNARE_PATH, getalldirectories(DYNARE_PATH)); 59 MATLAB_ROUTINES = {}; 60 for i=1:position(idDYNARE, MATLAB_PATH) 61 TMP_MATLAB_ROUTINES = getallroutinenames(MATLAB_PATH_{i}); 62 MATLAB_ROUTINES = { MATLAB_ROUTINES{:} TMP_MATLAB_ROUTINES{:} }; 63 end 64 COMMON_ROUTINES = intersect(MATLAB_ROUTINES, DYNARE_ROUTINES); 65 if ~isempty(COMMON_ROUTINES) 66 warning off backtrace 67 skipline() 68 if length(COMMON_ROUTINES)==1 69 warning(sprintf('%s This can cause problems because the Dynare implementation of %s will be overriden.', str0, COMMON_ROUTINES{1})); 70 else 71 str1 = repmat('%s, ', 1, length(COMMON_ROUTINES)-1); 72 str2 = 'and %s '; 73 str3 = sprintf(['%s This can cause problems because the Dynare implementations of ' str1, str2, 'will be overriden.'], str0, COMMON_ROUTINES{:}); 74 warning(str3); 75 end 76 if change_path_flag 77 skipline() 78 msg = sprintf(['I put %s on top of your Matlab/Ocatve path.' ... 79 '\nNote that this is a temporary change (i.e. it will not affect future Matlab/Octave sessions).\n' ... 80 '\nIf the ordering was intentional, i.e. if you really want to override the routines distributed with Dynare,' ... 81 ' you can change this behavior using the ''nopathchange'' option (see the reference manual).'], DYNARE_PATH); 82 warning(msg); 83 skipline() 84 rmpath(DYNARE_PATH) 85 addpath(DYNARE_PATH) 86 end 87 warning on backtrace 88 end 89 end 90 else 91 % Check that the user did not put all the subfolders in the path. 92 % => If DYNARE_PATH/qz is in the path while mjdgges dll is available 93 % it most likely means that user wrongly put all subfolders in the 94 % matlab's path! 95 mexpath = add_path_to_mex_files([DYNARE_PATH filesep], false); 96 MATLAB_PATH = path2cell(MATLAB_PATH); 97 for i=1:length(mexpath) 98 if exist([mexpath{i} filesep 'mjdgges.' mexext],'file') && ismember([DYNARE_PATH filesep 'qz'],MATLAB_PATH) 99 msg = sprintf(['You put all the subfolders of the Dynare matlab folder in the Matlab/Octave path! Only ' ... 100 'the dynare ' filesep 'matlab folder (without subfolders)\nshould be in the ' ... 101 'path. Dynare automatically adds any required subfolders to the ' ... 102 'path.']); 103 error(msg) 104 end 105 end 106 end 107end 108 109function q = path2cell(p) 110% Converts the output of path() to a cell 111s = strfind(p,pathsep); 112n = length(s)+1; 113q = cell(n,1); 114q(1) = {p(1:s(1)-1)}; 115q(n) = {p(s(end)+1:end)}; 116for i=2:n-1 117 q(i) = {p(s(i-1)+1:s(i)-1)}; 118end 119 120function flist = getallroutinenames(p, excludedsubfolders) 121if nargin<2 122 excludedsubfolders = {}; 123end 124flist={}; 125%get m-files in this directory 126dd = dir([p,filesep '*.m']); 127temp=struct2cell(dd); 128flist=[flist temp(1,:)]; 129%deal with subdirectories 130dlist=getalldirectories(p,excludedsubfolders); %first call with excluded directories 131for ii=1:length(dlist) 132 flist=[flist getallroutinenames([ p filesep dlist{ii}])]; %recursive calls without subfolders 133end 134 135 136function dlist = getalldirectories(p,excluded_directories) 137if nargin<2 138 excluded_directories = {}; 139end 140dd = dir(p); 141dir_result=struct2cell(dd); 142directory_indicator=cell2mat(dir_result(4,:)); 143dlist = dir_result(1,directory_indicator==1 & ~strcmp('.',dir_result(1,:)) & ~strcmp('..',dir_result(1,:)) & ~ismember(dir_result(1,:),excluded_directories)); 144 145function n = position(i, currentpath) 146n = length(strfind(currentpath(1:i), pathsep));