diff --git a/doc/internals/build_internal_documentation.m b/doc/internals/build_internal_documentation.m
index cbb83e6b9aa69a506f8700f4fee125117a2c2472..9dfe97a13475f1f766c1f33541339d840a70b3b7 100644
--- a/doc/internals/build_internal_documentation.m
+++ b/doc/internals/build_internal_documentation.m
@@ -15,7 +15,7 @@ simulationfiles = [];
 miscfiles = [];
 
 % Data.
-if exist('data.texi')
+if isfile('data.texi')
     delete('data.texi')
 end
 fid = fopen('data.texi','w');
@@ -32,7 +32,7 @@ end
 fclose(fid);
 
 % Estimation.
-if exist('estimation.texi')
+if isfile('estimation.texi')
     delete('estimation.texi')
 end
 fid = fopen('estimation.texi','w');
@@ -49,7 +49,7 @@ end
 fclose(fid);
 
 % Simulation.
-if exist('simulation.texi')
+if isfile('simulation.texi')
     delete('simulation.texi')
 end
 fid = fopen('simulation.texi','w');
@@ -66,7 +66,7 @@ end
 fclose(fid);
 
 % Miscellaneous.
-if exist('misc.texi')
+if isfile('misc.texi')
     delete('misc.texi')
 end
 fid = fopen('misc.texi','w');
diff --git a/matlab/+gsa/monte_carlo_filtering.m b/matlab/+gsa/monte_carlo_filtering.m
index 69b100b4d39813051f8fce6efb5af7a45a7c4bb2..bc653f22fcea6bd40fe5a2cc88cb101fe92ca101 100644
--- a/matlab/+gsa/monte_carlo_filtering.m
+++ b/matlab/+gsa/monte_carlo_filtering.m
@@ -115,7 +115,7 @@ if options_.opt_gsa.ppost
     xparam1_mean=c.xparam1;
     xparam1=c.xparam1;
     clear c
-elseif ~isempty(options_.mode_file) && exist([M_.dname filesep 'Output' filesep fname_,'_mean.mat'],'file')==2
+elseif ~isempty(options_.mode_file) && isfile([M_.dname filesep 'Output' filesep fname_,'_mean.mat'])
     c=load([M_.dname filesep 'Output' filesep fname_,'_mean.mat'],'xparam1');
     xparam1_mean=c.xparam1;
     xparam1=c.xparam1;
diff --git a/matlab/+gsa/run.m b/matlab/+gsa/run.m
index 8ff035e895a41ecf9f2bac1c56e0417479a169c9..f1726ba7abe21a55d4c64ac6d98f8c1fc4adf04a 100644
--- a/matlab/+gsa/run.m
+++ b/matlab/+gsa/run.m
@@ -284,8 +284,8 @@ else
 end
 
 if (options_gsa.load_stab || options_gsa.load_rmse || options_gsa.load_redform) && options_gsa.pprior
-    filetoload=[OutputDirectoryName '/' fname_ '_prior.mat'];
-    if ~exist(filetoload,'file')
+    filetoload=[OutputDirectoryName filesep fname_ '_prior.mat'];
+    if ~isfile(filetoload)
         disp([filetoload,' not found!'])
         disp('You asked to load a non existent analysis')
         return
@@ -356,11 +356,11 @@ if options_gsa.redform && ~isempty(options_gsa.namendo)
     if options_gsa.morris==1
         gsa.reduced_form_screening(OutputDirectoryName,options_gsa, estim_params_, M_, oo_.dr, options_, bayestopt_);
     else
-        % check existence of the SS_ANOVA toolbox
+        % check existence of the SS_ANOVA toolbox % TODO: Check if this is documented somewhere.
         if isempty(options_gsa.threshold_redform) && ~(exist('gsa_sdp','file')==6 || exist('gsa_sdp','file')==2)
             fprintf('\nThe "SS-ANOVA-R: MATLAB Toolbox for the estimation of Smoothing Spline ANOVA models with Recursive algorithms" is missing.\n')
             fprintf('To obtain it, go to:\n\n')
-            fprintf('https://ec.europa.eu/jrc/en/macro-econometric-statistical-software/ss-anova-r-downloads \n\n')
+            fprintf('https://ec.europa.eu/jrc/en/macro-econometric-statistical-software/ss-anova-r-downloads \n\n') % TODO: Check the link (the ss-anova toolbox doesn't seem to be available)
             fprintf('and follow the instructions there.\n')
             fprintf('After obtaining the files, you need to unpack them and set a Matlab Path to those files.\n')
             error('SS-ANOVA-R Toolbox missing!')
@@ -400,7 +400,7 @@ if options_gsa.rmse
             else
                 TmpDirectoryName = ([M_.dname filesep 'gsa' filesep 'mc']);
             end
-            if exist(TmpDirectoryName,'dir')
+            if isfolder(TmpDirectoryName)
                 mydelete([M_.fname '_filter_step_ahead*.mat'],[TmpDirectoryName filesep]);
                 mydelete([M_.fname '_inno*.mat'],[TmpDirectoryName filesep]);
                 mydelete([M_.fname '_smooth*.mat'],[TmpDirectoryName filesep]);
diff --git a/matlab/+identification/get_perturbation_params_derivs.m b/matlab/+identification/get_perturbation_params_derivs.m
index 354d25026be17064bc9239ca5be201393fa276dd..6800086a904c036a2232a1e1b5b4fbec02aabc59 100644
--- a/matlab/+identification/get_perturbation_params_derivs.m
+++ b/matlab/+identification/get_perturbation_params_derivs.m
@@ -448,11 +448,11 @@ if analytic_derivation_mode == -2
     clear dYss_g
 
 elseif (analytic_derivation_mode == 0 || analytic_derivation_mode == 1)
-    if ~exist(['+' fname filesep 'static_params_derivs.m'],'file')
-        error('For analytical parameter derivatives ''static_params_derivs.m'' file is needed, this can be created by putting identification(order=%d) into your mod file.',order)
+    if ~isfile(['+' fname filesep 'static_params_derivs.m'])
+        error('For analytical parameter derivatives ''static_params_derivs.m'' file is needed, this can be created by putting identification(order=%d) into your mod file.', order)
     end
-    if ~exist(['+' fname filesep 'dynamic_params_derivs.m'],'file')
-        error('For analytical parameter derivatives ''dynamic_params_derivs.m'' file is needed, this can be created by putting identification(order=%d) into your mod file.',order)
+    if ~isfile(['+' fname filesep 'dynamic_params_derivs.m'])
+        error('For analytical parameter derivatives ''dynamic_params_derivs.m'' file is needed, this can be created by putting identification(order=%d) into your mod file.', order)
     end
     %% Analytical computation of Jacobian and Hessian (wrt selected model parameters) of steady state, i.e. dYss and d2Yss
     [g1_static, T_order_static, T_static] = feval([fname,'.sparse.static_g1'], ys, exo_steady_state', params, M_.static_g1_sparse_rowval, M_.static_g1_sparse_colval, M_.static_g1_sparse_colptr); %g1_static is [endo_nbr by endo_nbr] first-derivative (wrt all endogenous variables) of static model equations f, i.e. df/dys, in declaration order
diff --git a/matlab/+mom/check_irf_matching_file.m b/matlab/+mom/check_irf_matching_file.m
index 3aa5db2726446db02de2cb6b6686b08a9806d208..dca955b716a3a761c722f12cc7c7d6e155d9746b 100644
--- a/matlab/+mom/check_irf_matching_file.m
+++ b/matlab/+mom/check_irf_matching_file.m
@@ -51,7 +51,7 @@ else
     if isempty(irf_matching_file_path)
         irf_matching_file_path = '.';
     end
-    if exist([irf_matching_file_path filesep irf_matching_file_name irf_matching_file_ext],'file') ~= 2
-        error('method_of_moments: Could not find a ''irf_matching_file'' called ''%s''!',[irf_matching_file_path filesep irf_matching_file_name irf_matching_file_ext]);
+    if ~isfile(fullfile(irf_matching_file_path, strcat(irf_matching_file_name, irf_matching_file_ext)))
+        error('method_of_moments: Could not find a ''irf_matching_file'' called ''%s''!', fullfile(irf_matching_file_path, strcat(irf_matching_file_name, irf_matching_file_ext)))
     end
-end
\ No newline at end of file
+end
diff --git a/matlab/+mom/default_option_mom_values.m b/matlab/+mom/default_option_mom_values.m
index e5f056a04cfc9c17d870ca0ca1fecd84d7c9dce6..661d9f285868e5926d46f47ab98d3b71f5ff1dba 100644
--- a/matlab/+mom/default_option_mom_values.m
+++ b/matlab/+mom/default_option_mom_values.m
@@ -460,7 +460,7 @@ if strcmp(mom_method,'IRF_MATCHING') && do_bayesian_estimation
         warning('method_of_moments: You specified mh_tune_jscale, but the maximum number of iterations is smaller than the step size. No update will take place.')
     end
     if options_mom_.load_results_after_load_mh
-        if ~exist([options_mom_.dirname filesep 'method_of_moments' filesep fname '_mom_results.mat'],'file')
+        if ~isfile(sprintf('%s%smethod_of_moments%s%s_mom_results.mat', options_mom.dirname, filesep, filesep, fname))
             fprintf('\nYou specified the ''load_results_after_load_mh'' option, but no ''%s_mom_results.mat'' file\n',fname);
             fprintf('was found in the folder %s%smethod_of_moments.\n',options_mom_.dirname,filesep);
             fprintf('Results will be recomputed and option ''load_results_after_load_mh'' is reset to false.\n');
@@ -477,6 +477,6 @@ if options_mom_.mom.analytic_jacobian && ~strcmp(mom_method,'GMM')
 end
 if strcmp(options_mom_.mom.mom_method,'IRF_MATCHING')
     if any(cellfun(@(x) isnumeric(x) && any(x == 13), options_mom_.optimizer_vec))
-        error('method_of_moments: lsqnonlin (mode_compute=13) is not yet supported for IRF matching!');
+        error('method_of_moments: lsqnonlin (mode_compute=13) is not yet supported for IRF matching!')
     end
-end
\ No newline at end of file
+end
diff --git a/matlab/+mom/run.m b/matlab/+mom/run.m
index 93f5e2335f432b92331d6772e5961719f49adaf0..e06d9c7feac931665233184dc61e3386dc4f4bbe 100644
--- a/matlab/+mom/run.m
+++ b/matlab/+mom/run.m
@@ -281,7 +281,7 @@ if options_mom_.mom.mom_nbr < length(xparam0)
 end
 skipline(2);
 % check if a _prior_restrictions.m file exists
-if exist([M_.fname '_prior_restrictions.m'],'file')
+if isfile(sprintf('%s_prior_restrictions.m', M_.fname))
     options_mom_.prior_restrictions.status = 1;
     options_mom_.prior_restrictions.routine = str2func([M_.fname '_prior_restrictions']);
 end
@@ -828,4 +828,4 @@ if strcmp(options_mom_.mom.mom_method,'SMM') || strcmp(options_mom_.mom.mom_meth
 end
 if strcmp(options_mom_.mom.mom_method,'IRF_MATCHING') && ~isempty(options_mom_.mom.irf_matching_file.path) && ~strcmp(options_mom_.mom.irf_matching_file.path,'.')
     rmpath(options_mom_.irf_matching_file.path); % remove path to irf_matching_file
-end
\ No newline at end of file
+end
diff --git a/matlab/+occbin/graph.m b/matlab/+occbin/graph.m
index 187a1e5f34f1219aadfa5bf820a2b0ca9f5c2935..b2ff74b2c1142ae33b5647e5278099bb19b15e48 100644
--- a/matlab/+occbin/graph.m
+++ b/matlab/+occbin/graph.m
@@ -27,7 +27,7 @@ function graph(M_, options_, options_occbin_, oo_, var_list)
 
 options_ = occbin.set_option(options_,options_occbin_,'graph.steady_state');
 
-if ~exist([M_.dname '/graphs'],'dir')
+if ~isfolder(sprintf('%s/graphs', M_.dname))
     mkdir(M_.dname,'graphs');
 end
 
diff --git a/matlab/aggregate.m b/matlab/aggregate.m
index b8ae195b04f2703bd52f722f7a79f1098d40c1d7..4555ed27b4dd27c686a5b13a4dc610be7d8e6171 100644
--- a/matlab/aggregate.m
+++ b/matlab/aggregate.m
@@ -254,13 +254,13 @@ xlist = [xlist1, xlist2];
 
 % Print all cherry-picked models in one mod-file.
 [filepath, filename, fileext] = fileparts(ofile);
-if ~isempty(filepath) && ~exist(filepath, 'dir')
+if ~isempty(filepath) && ~isfolder(filepath)
     mkdir(filepath);
 end
 if isempty(filepath)
     fid = fopen(sprintf('%s%s', filename, fileext), 'w');
 else
-    fid = fopen(sprintf('%s%s%s%s', filepath, filesep(), filename, fileext), 'w');
+    fid = fopen(fullfile(filepath, strcat(filename, fileext)), 'w');
 end
 if ~isempty(firstline)
     fprintf(fid, '%s\n\n', firstline);
diff --git a/matlab/backward/checkdatabase.m b/matlab/backward/checkdatabase.m
index daeb14aecc544c3d3c789c779190ab4623dfeccf..5a36f38dc62d4d0c604971ed91f57186fd77c855 100644
--- a/matlab/backward/checkdatabase.m
+++ b/matlab/backward/checkdatabase.m
@@ -31,7 +31,7 @@ if nargin<3
     inversionflag = false;
 end
 
-if exist(sprintf('+%s/dynamic_set_auxiliary_series.m', M_.fname), 'file')
+if isfile(sprintf('+%s/dynamic_set_auxiliary_series.m', M_.fname))
     dbase = feval(sprintf('%s.dynamic_set_auxiliary_series', M_.fname), dbase, M_.params);
 end
 
diff --git a/matlab/backward/shock_decomposition_backward.m b/matlab/backward/shock_decomposition_backward.m
index 9296200606a2598374f4bc7dbc6e2f62e11fd866..ec23f586b0446ea111b8ce801f274999ef0f6ecc 100644
--- a/matlab/backward/shock_decomposition_backward.m
+++ b/matlab/backward/shock_decomposition_backward.m
@@ -59,7 +59,7 @@ nperiods = simulations.nobs - initialconditions.nobs;
 decomposition = NaN(M_.endo_nbr, length(shocklist), nperiods);
 
 % Add auxiliary variables to simulation paths
-if exist(sprintf('+%s/dynamic_set_auxiliary_series.m', M_.fname), 'file')
+if isfile(sprintf('+%s/dynamic_set_auxiliary_series.m', M_.fname))
     simulations = feval(sprintf('%s.dynamic_set_auxiliary_series', M_.fname), simulations, M_.params);
 end
 
@@ -89,7 +89,7 @@ for i = length(shocklist):-1:1
 
     % Compute simulation with the current shock or shock group removed
     simulations_new = simul_backward_model(initialconditions, nperiods, innovations);
-    if exist(sprintf('+%s/dynamic_set_auxiliary_series.m', M_.fname), 'file')
+    if isfile(sprintf('+%s/dynamic_set_auxiliary_series.m', M_.fname))
         simulations_new = feval(sprintf('%s.dynamic_set_auxiliary_series', M_.fname), simulations_new, M_.params);
     end
 
diff --git a/matlab/check_matlab_path.m b/matlab/check_matlab_path.m
index 79bf393d9a4568e0f7bd4c314a3d6846eabd2e46..d3a492896a27e454b08a216e1e1e7c662a866ed9 100644
--- a/matlab/check_matlab_path.m
+++ b/matlab/check_matlab_path.m
@@ -95,7 +95,7 @@ else
         mexpath = get_path_to_mex_files([DYNARE_PATH filesep]);
         MATLAB_PATH = path2cell(MATLAB_PATH);
         for i=1:length(mexpath)
-            if exist([mexpath{i} filesep 'mjdgges.' mexext],'file') && ismember([DYNARE_PATH filesep 'qz'],MATLAB_PATH)
+            if isfile([mexpath{i} filesep 'mjdgges.' mexext]) && ismember([DYNARE_PATH filesep 'qz'],MATLAB_PATH)
                 msg = sprintf(['You put all the subfolders of the Dynare matlab folder in the Matlab/Octave path! Only ' ...
                                'the dynare ' filesep 'matlab folder (without subfolders)\nshould be in the ' ...
                                'path. Dynare automatically adds any required subfolders to the ' ...
diff --git a/matlab/cherrypick.m b/matlab/cherrypick.m
index 1aac16614cfd829d6550242d97897a3f5f92b261..1f8d5b1005140e8d04eb7fb4727234201f3c3b69 100644
--- a/matlab/cherrypick.m
+++ b/matlab/cherrypick.m
@@ -52,10 +52,10 @@ end
 mkdir(outfold);
 
 % Check that infile.mod and the related JSON output exist.
-if ~exist(sprintf('%s.mod', infile), 'file')
+if ~isfile(sprintf('%s.mod', infile))
     error('Cannot find %s.mod.', infile)
 end
-if ~exist(sprintf('%s/model/json', infile), 'dir')
+if ~isfolder(sprintf('%s/model/json', infile))
     error('Cannot find %s/model/json folder. Did you run %s.mod with the json option?', infile, infile);
 end
 
diff --git a/matlab/cli/search.m b/matlab/cli/search.m
index ff391c2a6166eb5fd24e6986f96a915c43824206..c5774904715ff6ba6a215b0dbe0eb4b8732ccb3e 100644
--- a/matlab/cli/search.m
+++ b/matlab/cli/search.m
@@ -37,7 +37,7 @@ end
 jname = sprintf('%s%smodel%sjson%smodfile-original.json', M_.fname, filesep, filesep, filesep);
 
 % Check if corresponding JSON file exists.
-if exist(jname, 'file') ~= 2
+if ~isfile(jname, 'file')
     error('Please use the json option (See the Dynare invocation section in the reference manual).');
 end
 
diff --git a/matlab/convergence_diagnostics/mcmc_diagnostics_core.m b/matlab/convergence_diagnostics/mcmc_diagnostics_core.m
index 65ebd74f057cecb6aabc045bdb62dec0ef55b022..dbee0d913612e63d8ff0d7c34920824d094a3957 100644
--- a/matlab/convergence_diagnostics/mcmc_diagnostics_core.m
+++ b/matlab/convergence_diagnostics/mcmc_diagnostics_core.m
@@ -70,7 +70,7 @@ M_=myinputs.M_;
 if whoiam
     Parallel=myinputs.Parallel;
 end
-if ~exist(MetropolisFolder,'dir')
+if ~isfolder(MetropolisFolder)
     MetropolisFolder = CheckPath('metropolis',M_.dname);
 end
 
@@ -142,4 +142,4 @@ for j=fpar:npar
     end
 end
 
-myoutput.UDIAG = UDIAG;
\ No newline at end of file
+myoutput.UDIAG = UDIAG;
diff --git a/matlab/default_option_values.m b/matlab/default_option_values.m
index d3773fc9377e6432e8e1689878908ca1e5743213..92123f72ba07e7ae6bec22d70350015edbab95df 100644
--- a/matlab/default_option_values.m
+++ b/matlab/default_option_values.m
@@ -81,9 +81,9 @@ options_.threads.k_order_perturbation = max(1, num_procs/2);
 options_.jacobian_flag = true;
 
 % steady state file
-if exist(['+' M_.fname '/steadystate.m'],'file')
+if isfile(sprintf('+%s%ssteadystate.m', M_.fname, filesep))
     options_.steadystate_flag = 2;
-elseif exist([M_.fname '_steadystate.m'],'file')
+elseif isfile(sprintf('%s_steadystate.m', M_.fname))
     options_.steadystate_flag = 1;
 else
     options_.steadystate_flag = 0;
diff --git a/matlab/dynare.m b/matlab/dynare.m
index 5d191527943533bb8253ad523fbbb15a52dee23d..ce973cd45e72292d98a35647e285afc4dae236f9 100644
--- a/matlab/dynare.m
+++ b/matlab/dynare.m
@@ -141,7 +141,7 @@ end
 if contains(fname,filesep)
     fprintf('\nIt seems you are trying to call a .mod file not located in the "Current Folder". This is not possible (the %s symbol is not allowed in the name of the .mod file).\n', filesep)
     [pathtomodfile,basename] = fileparts(fname);
-    if exist(pathtomodfile,'dir')
+    if isfolder(pathtomodfile)
         filesindirectory = dir(pathtomodfile);
         filesindirectory = struct2cell(filesindirectory);
         filesindirectory = filesindirectory(1,:);
@@ -155,7 +155,7 @@ if contains(fname,filesep)
     error(['Dynare: can''t open ' fname, '.'])
 end
 
-if ~exist(fname,'file') || isequal(fname,'dir')
+if ~isfile(fname) || isequal(fname,'dir')
     fprintf('\nThe file %s could not be located in the "Current Folder". Check whether you typed in the correct filename\n',fname)
     fprintf('and whether the file is really located in the "Current Folder".\n')
     try
@@ -173,7 +173,7 @@ if ~isvarname(fname(1:end-4))
 end
 
 % pre-dynare-preprocessor-hook
-if exist(fname(1:end-4),'dir') && exist([fname(1:end-4) filesep 'hooks'],'dir') && exist([fname(1:end-4) filesep 'hooks/priorprocessing.m'],'file')
+if isfolder(fname(1:end-4)) && isfolder([fname(1:end-4) filesep 'hooks']) && isfile([fname(1:end-4) filesep 'hooks/priorprocessing.m'])
     run([fname(1:end-4) filesep 'hooks/priorprocessing'])
 end
 
@@ -190,7 +190,7 @@ fast = ismember('fast', varargin) || ismember('fast', file_opts);
 diary off
 if ~nolog
     logfile = [ fname(1:end-4) '.log' ];
-    if exist(logfile, 'file')
+    if isfile(logfile)
         delete(logfile)
     end
     diary(logfile)
@@ -239,7 +239,7 @@ end
 % For an instance of this bug, see:
 % https://forum.dynare.org/t/issue-with-dynare-preprocessor-4-6-1/15448/1
 if ~fast
-    if ispc && ~isoctave && exist(['+',fname(1:end-4)],'dir')
+    if ispc && ~isoctave && isfolder(['+',fname(1:end-4)])
         [~,~]=rmdir(['+', fname(1:end-4)],'s'); % output required to suppress triggering error
     end
 end
@@ -287,7 +287,7 @@ if onlyjson
 end
 
 % post-dynare-prerocessor-hook
-if exist(fname(1:end-4),'dir') && exist([fname(1:end-4) filesep 'hooks'],'dir') && exist([fname(1:end-4) filesep 'hooks/postprocessing.m'],'file')
+if isfolder(fname(1:end-4)) && isfolder([fname(1:end-4) filesep 'hooks']) && isfile([fname(1:end-4) filesep 'hooks/postprocessing.m'])
     run([fname(1:end-4) filesep 'hooks/postprocessing'])
 end
 
diff --git a/matlab/estimation/check_posterior_sampler_options.m b/matlab/estimation/check_posterior_sampler_options.m
index e93510075baf1f6956c47beb1a5c148f9eefc3a7..acb06e07070f8677029c7b421d77606a33d76942 100644
--- a/matlab/estimation/check_posterior_sampler_options.m
+++ b/matlab/estimation/check_posterior_sampler_options.m
@@ -92,7 +92,7 @@ if init
                   case 'scale_file'
                     % load optimal_mh_scale parameter if previous run was with mode_compute=6
                     % will overwrite jscale from set_prior.m
-                    if exist(options_list{i,2},'file') || exist([options_list{i,2},'.mat'],'file')
+                    if isfile(options_list{i,2}) || isfile([options_list{i,2},'.mat'])
                         tmp = load(options_list{i,2},'Scale');
                         bayestopt_.mh_jscale = tmp.Scale;
                         options_.mh_jscale = tmp.Scale;
@@ -157,7 +157,7 @@ if init
                   case 'scale_file'
                     % load optimal_mh_scale parameter if previous run was with mode_compute=6
                     % will overwrite jscale from set_prior.m
-                    if exist(options_list{i,2},'file') || exist([options_list{i,2},'.mat'],'file')
+                    if isfile(options_list{i,2}) || isfile([options_list{i,2},'.mat'])
                         tmp = load(options_list{i,2},'Scale');
                         bayestopt_.mh_jscale = tmp.Scale;
                         options_.mh_jscale = tmp.Scale;
diff --git a/matlab/estimation/check_prior_analysis_data.m b/matlab/estimation/check_prior_analysis_data.m
index ee400509870971f196387a15b016014c31d056f5..a60556caf0b5a62143850aca36af6161aa663dc4 100644
--- a/matlab/estimation/check_prior_analysis_data.m
+++ b/matlab/estimation/check_prior_analysis_data.m
@@ -41,7 +41,7 @@ if nargout>1
 end
 
 %% Get informations about prior draws files.
-if ~exist([ M_.dname '/prior/draws'],'dir')
+if ~isfolder(sprintf('%s%sprior%sdraws', M_.dname, filesep, filesep))
     disp('check_prior_analysis_data:: Can''t find any prior draws file!')
     return
 end
diff --git a/matlab/estimation/delete_mh_history_files.m b/matlab/estimation/delete_mh_history_files.m
index c626b577bdbde3dd9cf8c3d1714b79d34b93a202..fef6e55f0c3a357293cc0434942cf24ca8b1e698 100644
--- a/matlab/estimation/delete_mh_history_files.m
+++ b/matlab/estimation/delete_mh_history_files.m
@@ -24,9 +24,9 @@ mh_history_files = dir([BaseName '_mh_history_*.mat']);
 
 % Delete the mh-history files (old or new format) if any.
 if isequal(length(mh_history_files),0)
-    if exist([BaseName '_mh_history.mat'])
+    if isfile([BaseName '_mh_history.mat'])
         delete([BaseName '_mh_history.mat'])
     end
 else
     delete([BaseName '_mh_history_*.mat'])
-end
\ No newline at end of file
+end
diff --git a/matlab/estimation/dynare_estimation_1.m b/matlab/estimation/dynare_estimation_1.m
index 6e2bd2bee0d2b881644be389f367d46fb99acd8e..17699fa1d765bc1fa3ac74bf3994fce221451394 100644
--- a/matlab/estimation/dynare_estimation_1.m
+++ b/matlab/estimation/dynare_estimation_1.m
@@ -45,8 +45,8 @@ if isdime(options_) && options_.prior_trunc
     fprintf('%s: DIME requires no prior truncation. Resetting options_.prior_trunc=0.\n', dispString);
 end
 
-if ~exist([M_.dname filesep 'Output'],'dir')
-    mkdir(M_.dname,'Output');
+if ~isfolder(sprintf('%s%sOutput', M_.dname, filesep))
+    mkdir(M_.dname, 'Output');
 end
 
 if isempty(estim_params_)
diff --git a/matlab/estimation/dynare_estimation_init.m b/matlab/estimation/dynare_estimation_init.m
index 1303f810730fd72e1a27b5f257d56171ac90935f..fdfb92f95defd0b15361229c822c0826987b27a4 100644
--- a/matlab/estimation/dynare_estimation_init.m
+++ b/matlab/estimation/dynare_estimation_init.m
@@ -147,7 +147,7 @@ if ~isempty(bayestopt_) && any(bayestopt_.pshape==0) && any(bayestopt_.pshape~=0
     error('Estimation must be either fully ML or fully Bayesian. Maybe you forgot to specify a prior distribution.')
 end
 % Check if a _prior_restrictions.m file exists
-if exist([M_.fname '_prior_restrictions.m'],'file')
+if isfile(sprintf('%s_prior_restrictions.m', M_.fname))
     options_.prior_restrictions.status = 1;
     options_.prior_restrictions.routine = str2func([M_.fname '_prior_restrictions']);
 end
@@ -476,7 +476,7 @@ end
 estim_params_= get_matrix_entries_for_psd_check(M_,estim_params_);
 
 if options_.load_results_after_load_mh
-    if ~exist([M_.dname filesep 'Output' filesep M_.fname '_results.mat'],'file')
+    if ~isfile(sprintf('%s%sOutput%s%s_results.mat', M_.dname, filesep, filesep, M°.fname))
         fprintf('\ndynare_estimation_init:: You specified the load_results_after_load_mh, but no _results.mat-file\n')
         fprintf('dynare_estimation_init:: was found. Results will be recomputed.\n')
         options_.load_results_after_load_mh=0;
diff --git a/matlab/estimation/execute_prior_posterior_function.m b/matlab/estimation/execute_prior_posterior_function.m
index 898cf63005952007353f40404ec49f83ee749613..5bd2e98ed578d8603ac4ca5af93c13c8eabe784e 100644
--- a/matlab/estimation/execute_prior_posterior_function.m
+++ b/matlab/estimation/execute_prior_posterior_function.m
@@ -41,7 +41,7 @@ end
 fullname = [basename extension];
 if ~strcmp(extension,'.m') %if not m-file
     error('The Posterior Function is not an m-file.')
-elseif ~exist(fullname,'file') %if m-file, but does not exist
+elseif ~isfile(fullname) %if m-file, but does not exist
     error(['The Posterior Function ', fullname ,' was not found. Check the spelling.']);
 end
 %Create function handle
@@ -68,7 +68,7 @@ elseif strcmpi(type,'prior')
             error('The prior distributions are not properly set up.')
         end
     end
-    if exist([M_.fname '_prior_restrictions.m'],"file")
+    if isfile([M_.fname '_prior_restrictions.m'])
         warning('prior_function currently does not support endogenous prior restrictions. They will be ignored. Consider using a posterior_function with nobs=1.')
     end
     Prior = dprior(bayestopt_, options_.prior_trunc);
diff --git a/matlab/estimation/load_last_mh_history_file.m b/matlab/estimation/load_last_mh_history_file.m
index 27d25274c16a574b3a65483de2787578272f3b78..479ec3c7583e179312777f8dc8a1db61c32b50c0 100644
--- a/matlab/estimation/load_last_mh_history_file.m
+++ b/matlab/estimation/load_last_mh_history_file.m
@@ -34,7 +34,7 @@ mh_history_files = dir([BaseName '_mh_history_*.mat']);
 
 % Consistency with older versions of Dynare.
 if isequal(length(mh_history_files),0)
-    if exist([BaseName '_mh_history.mat'],'file')
+    if isfile([BaseName '_mh_history.mat'])
         format_mh_history_file = 1; % old Dynare format
     else
         error(['Estimation::load_mh_file: I cannot find any mh-history file in ' MetropolisFolder '!'])
@@ -66,4 +66,4 @@ else
     if ~isfield(record,'MAX_nruns')
         record.MAX_nruns=NaN(size(record.MhDraws,1),1); % This information is forever lost...
     end
-end
\ No newline at end of file
+end
diff --git a/matlab/estimation/mh_autocorrelation_function.m b/matlab/estimation/mh_autocorrelation_function.m
index 04ef6fb2c0c848a25dfbdd10b2b2a62f9c6871cb..8f24711885ff9de912dd38ab53beaefd7f50702e 100644
--- a/matlab/estimation/mh_autocorrelation_function.m
+++ b/matlab/estimation/mh_autocorrelation_function.m
@@ -84,11 +84,11 @@ hh_fig=dyn_figure(options_.nodisplay,'Name',FigureName);
 bar(0:options_.mh_autocorrelation_function_size,autocor,'k');
 axis tight
 % create subdirectory <dname>/graphs if it doesn't exist
-if ~exist(M_.dname, 'dir')
-    mkdir('.',M_.dname);
+if ~isfolder(M_.dname)
+    mkdir('.', M_.dname);
 end
-if ~exist([M_.dname filesep 'graphs'],'dir')
-    mkdir(M_.dname,'graphs');
+if ~isfolder(sprintf('%s%sgraphs', M_.dname, filesep))
+    mkdir(M_.dname, 'graphs');
 end
 
 plot_name=get_the_name(column,0,M_,estim_params_,options_.varobs);
diff --git a/matlab/estimation/posterior_sampler_core.m b/matlab/estimation/posterior_sampler_core.m
index 61492fd4fe09369cfa62f66a698076cf9bc1f2b9..8ac57b46a0c2607669f5282ca7300b059cf7176e 100644
--- a/matlab/estimation/posterior_sampler_core.m
+++ b/matlab/estimation/posterior_sampler_core.m
@@ -135,7 +135,7 @@ for curr_block = fblck:nblck
         options_=set_dynare_seed_local_options(options_,options_.DynareRandomStreams.seed+curr_block);
     end
     mh_recover_flag=0;
-    if options_.mh_recover && exist([BaseName '_mh_tmp_blck' int2str(curr_block) '.mat'],'file')==2 && OpenOldFile(curr_block)
+    if options_.mh_recover && isfile([BaseName '_mh_tmp_blck' int2str(curr_block) '.mat']) && OpenOldFile(curr_block)
         % this should be done whatever value of load_mh_file
         load([BaseName '_mh_tmp_blck' int2str(curr_block) '.mat']);
         draw_iter = size(neval_this_chain,2)+1;
diff --git a/matlab/estimation/set_prior.m b/matlab/estimation/set_prior.m
index 3069a8a09598ace5870d106aee51f254b80af1d9..238aca5cd3a47af1ad587884625a73a73b92a594 100644
--- a/matlab/estimation/set_prior.m
+++ b/matlab/estimation/set_prior.m
@@ -285,24 +285,25 @@ end
 CheckPath('prior',M_.dname);
 
 % I save the prior definition if the prior has changed.
-if exist([ M_.dname '/prior/definition.mat'],'file')
-    old = load([M_.dname '/prior/definition.mat'],'bayestopt_');
-    prior_has_changed = 0;
-    if length(bayestopt_.p1)==length(old.bayestopt_.p1)
-        if any(bayestopt_.p1-old.bayestopt_.p1)
-            prior_has_changed = 1;
-        elseif any(bayestopt_.p2-old.bayestopt_.p2)
-            prior_has_changed = 1;
-        elseif any(bayestopt_.p3-old.bayestopt_.p3)
-            prior_has_changed = 1;
-        elseif any(bayestopt_.p4-old.bayestopt_.p4)
-            prior_has_changed = 1;
-        elseif any(bayestopt_.p5-old.bayestopt_.p5(:))
-            prior_has_changed = 1;
-        elseif any(bayestopt_.p6-old.bayestopt_.p6)
-            prior_has_changed = 1;
-        elseif any(bayestopt_.p7-old.bayestopt_.p7)
-            prior_has_changed = 1;
+priorfile = sprintf('%s/prior/definition.mat', M_.dname);
+if isfile(priorfile)
+    old = load(priorfile, 'prior');
+    prior_has_changed = false;
+    if length(prior.p1)==length(old.prior.p1)
+        if any(prior.p1-old.prior.p1)
+            prior_has_changed = true;
+        elseif any(prior.p2-old.prior.p2)
+            prior_has_changed = true;
+        elseif any(prior.p3-old.prior.p3)
+            prior_has_changed = true;
+        elseif any(prior.p4-old.prior.p4)
+            prior_has_changed = true;
+        elseif any(prior.p5-old.prior.p5(:))
+            prior_has_changed = true;
+        elseif any(prior.p6-old.prior.p6)
+            prior_has_changed = true;
+        elseif any(prior.p7-old.prior.p7)
+            prior_has_changed = true;
         end
     else
         prior_has_changed = 1;
diff --git a/matlab/estimation/trace_plot.m b/matlab/estimation/trace_plot.m
index 2d0dd7c2d6e2c6edbfaa6b2771d1e5157c56d4d1..861f9d8f99d1b34d80b425e32f96242da35e443d 100644
--- a/matlab/estimation/trace_plot.m
+++ b/matlab/estimation/trace_plot.m
@@ -142,11 +142,11 @@ else
     legend(pp,strcat(repmat({'Chain '},n_nblocks_to_plot,1),num2str(blck(:))));
 end
 % create subdirectory <dname>/graphs if it doesn't exist
-if ~exist(M_.dname, 'dir')
-    mkdir('.',M_.dname);
+if ~isfolder(M_.dname)
+    mkdir('.', M_.dname);
 end
-if ~exist([M_.dname filesep 'graphs'],'dir')
-    mkdir(M_.dname,'graphs');
+if ~isfolder(sprintf('%s%sgraphs', M_.dname, filesep))
+    mkdir(M_.dname, 'graphs');
 end
 
 %get name for plot
diff --git a/matlab/forecast_graphs.m b/matlab/forecast_graphs.m
index 3b26838ec396ac2a635aea2529cb1d0669e58b76..4c7395f23a10dc3525366d6a4819d303c43d4b2b 100644
--- a/matlab/forecast_graphs.m
+++ b/matlab/forecast_graphs.m
@@ -48,11 +48,11 @@ end
 nvar = length(i_var);
 
 % create subdirectory <dname>/graphs if id doesn't exist
-if ~exist(dname, 'dir')
-    mkdir('.',dname);
+if ~isfolder(dname)
+    mkdir('.', dname);
 end
-if ~exist([dname '/graphs'],'dir')
-    mkdir(dname,'graphs');
+if ~isfolder(sprintf('%s%sgraphs', dname, filesep))
+    mkdir(dname, 'graphs');
 end
 
 if options_.TeX && any(strcmp('eps', cellstr(options_.graph_format)))
diff --git a/matlab/get_build_dir.m b/matlab/get_build_dir.m
index aa0e39ae3dcc2481070a503656fb2808968ea332..fb8b283d2f9cf0b22b3e604dbe8636244507721f 100644
--- a/matlab/get_build_dir.m
+++ b/matlab/get_build_dir.m
@@ -25,9 +25,9 @@ default_octave = [ dynareroot '..' filesep 'build-octave' ];
 
 if ~isempty(envvar)
     p = envvar;
-elseif ~isoctave && exist(default_matlab, 'dir')
+elseif ~isoctave && isfolder(default_matlab)
     p = default_matlab;
-elseif isoctave && exist(default_octave, 'dir')
+elseif isoctave && isfolder(default_octave)
     p = default_octave;
 else
     p = [];
diff --git a/matlab/get_path_to_mex_files.m b/matlab/get_path_to_mex_files.m
index 4823b37a114b86320dfd814ea777e321259aa6b3..b2cbf22af482253337a70b0751853c59ab7fe068 100644
--- a/matlab/get_path_to_mex_files.m
+++ b/matlab/get_path_to_mex_files.m
@@ -30,7 +30,7 @@ elseif isoctave
             warning('MEX files not available for 32-bit Octave')
         else
             tmp = [dynareroot '../mex/octave/win64/'];
-            if exist(tmp, 'dir')
+            if isfolder(tmp)
                 mexpath = tmp;
             end
         end
@@ -48,20 +48,20 @@ else
     % Add win64 specific paths for Dynare Windows package
     if strcmp(computer, 'PCWIN64')
         tmp = [dynareroot '../mex/matlab/win64-9.5-24.2/'];
-        if exist(tmp, 'dir')
+        if isfolder(tmp)
             mexpath = tmp;
         end
     end
     % Add macOS paths for Dynare Mac package
     if strcmp(computer, 'MACI64')
         tmp = [dynareroot '../mex/matlab/maci64-9.5-24.2/'];
-        if exist(tmp, 'dir')
+        if isfolder(tmp)
             mexpath = tmp;
         end
     end
     if strcmp(computer, 'MACA64')
         tmp = [dynareroot '../mex/matlab/maca64-23.2-24.2/'];
-        if exist(tmp, 'dir')
+        if isfolder(tmp)
             mexpath = tmp;
         end
     end
diff --git a/matlab/histvalf_initvalf.m b/matlab/histvalf_initvalf.m
index 36aaf6200502c6ddac171db34c5b5f9611fcbf6e..80ce2d74ca577349721c4800ebc2863cbbb47a2e 100644
--- a/matlab/histvalf_initvalf.m
+++ b/matlab/histvalf_initvalf.m
@@ -61,19 +61,19 @@ if datafile
     [~,basename,extension] = fileparts(datafile);
     % Auto-detect extension if not provided
     if isempty(extension)
-        if exist([basename '.m'],'file')
+        if isfile(sprintf('%s.m', basename))
             extension = '.m';
-        elseif exist([basename '.mat'],'file')
+        elseif isfile(sprintf('%s.mat', basename))
             extension = '.mat';
-        elseif exist([basename '.xls'],'file')
+        elseif isfile(sprintf('%s.xls', basename))
             extension = '.xls';
-        elseif exist([basename '.xlsx'],'file')
+        elseif isfile(sprintf('%s.xlsx', basename))
             extension = '.xlsx';
         else
             error('%s_FILE: Can''t find datafile: %s.{m,mat,xls,xlsx}', caller, basename);
         end
     end
-    fullname = [basename extension];
+    fullname = strcat(basename, extension);
     series = dseries(fullname);
 end
 
@@ -104,7 +104,7 @@ if error_flag
     error('%s_FILE: some variables are missing', caller)
 end
 
-if exist(sprintf('+%s/dynamic_set_auxiliary_series.m', M_.fname), 'file')
+if isfile(sprintf('+%s/dynamic_set_auxiliary_series.m', M_.fname))
     series = feval(sprintf('%s.dynamic_set_auxiliary_series', M_.fname), series, M_.params);
 end
 
diff --git a/matlab/kalman/plot_classical_smoother_results.m b/matlab/kalman/plot_classical_smoother_results.m
index 578d3166a146a40cbff6e9b57dde54fbeb61575c..0388b240accf019f5770a2018f6f180b81ca633e 100644
--- a/matlab/kalman/plot_classical_smoother_results.m
+++ b/matlab/kalman/plot_classical_smoother_results.m
@@ -33,7 +33,7 @@ n_varobs = length(options_.varobs);
 
 if ~options_.nograph
     [nbplt,nr,nc,~,~,nstar] = pltorg(M_.exo_nbr);
-    if ~exist([M_.dname '/graphs'],'dir')
+    if ~isfolder(sprintf('%s%sgraphs', M_.dname, filesep))
         mkdir(M_.dname,'graphs');
     end
     if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format)))
diff --git a/matlab/kalman/save_display_classical_smoother_results.m b/matlab/kalman/save_display_classical_smoother_results.m
index 46d42c99694af366a4b6df4cc0602eba134ec0ba..94a0f3eaa56e914f8aa9258054465509d4c5706e 100644
--- a/matlab/kalman/save_display_classical_smoother_results.m
+++ b/matlab/kalman/save_display_classical_smoother_results.m
@@ -63,4 +63,4 @@ else
 end
 if ~smoother_error
     plot_classical_smoother_results(M_,oo_,options_,dataset_info,dataset_,estim_params_,yf);
-end
\ No newline at end of file
+end
diff --git a/matlab/latex/write_latex_definitions.m b/matlab/latex/write_latex_definitions.m
index a2e836a0bcbd1271123bef0dbd5293d284210602..8009b7dd4cb847a153078d2d1bcd883455a7f80f 100644
--- a/matlab/latex/write_latex_definitions.m
+++ b/matlab/latex/write_latex_definitions.m
@@ -39,8 +39,8 @@ else
     M_var_root = {'M_.endo', 'M_.exo', 'M_.exo_det', 'M_.param'};
 end
 
-if ~exist([M_.dname '/latex'],'dir')
-    mkdir(M_.dname,'latex');
+if ~isfolder(sprintf('%s%s/latex', M_.dname , filesep))
+    mkdir(M_.dname, 'latex');
 end
 fid = fopen([M_.dname, '/latex/' M_.fname '_latex_definitions.tex'], 'w');
 for i=1:length(tables)
diff --git a/matlab/latex/write_latex_parameter_table.m b/matlab/latex/write_latex_parameter_table.m
index e92cfad84f883accc4af375b454aab521aa792a2..1ba22677f192b55ade0dcb08d8a15714ab195e08 100644
--- a/matlab/latex/write_latex_parameter_table.m
+++ b/matlab/latex/write_latex_parameter_table.m
@@ -37,8 +37,8 @@ if ~isequal(M_.param_names, M_.param_names_long)
     Long_names_present = true;
 end
 
-if ~exist([M_.dname '/latex'],'dir')
-    mkdir(M_.dname,'latex');
+if ~isfolder(sprintf('%s%slatex', M_.dname, filesep))
+    mkdir(M_.dname, 'latex');
 end
 
 fid = fopen([M_.dname, '/latex/' M_.fname '_latex_parameters.tex'], 'w');
diff --git a/matlab/latex/write_latex_prior_table.m b/matlab/latex/write_latex_prior_table.m
index 7d5c21067ae269aaf4c8ce53a36ee052d44e85ad..dcf8f51f820bbe1900f56b8947d4a8a52854b346 100644
--- a/matlab/latex/write_latex_prior_table.m
+++ b/matlab/latex/write_latex_prior_table.m
@@ -54,8 +54,8 @@ ub=bounds.ub;
 
 PriorNames = { 'Beta' , 'Gamma' , 'Gaussian' , 'Inv. Gamma' , 'Uniform' , 'Inv. Gamma -- 2', '', 'Weibull' };
 
-if ~exist([M_.dname '/latex'],'dir')
-    mkdir(M_.dname,'latex');
+if ~isfolder(sprintf('%s%slatex', M_.danme, filesep))
+    mkdir(M_.dname, 'latex');
 end
 fidTeX = fopen([M_.dname, '/latex/' M_.fname '_priors_table.tex'],'w+');
 fprintf(fidTeX,'%% TeX-table generated by Dynare write_latex_prior_table.m.\n');
@@ -196,4 +196,4 @@ if ~isnumeric(UpperBound)
 else
     format_string = [ format_string , ' %6.4f &'];
 end
-format_string = [ format_string , ' %6.4f & %6.4f \\\\ \n'];
\ No newline at end of file
+format_string = [ format_string , ' %6.4f & %6.4f \\\\ \n'];
diff --git a/matlab/load_m_file_data_legacy.m b/matlab/load_m_file_data_legacy.m
index d48bd4ade2f214d96183b2d84455672284909fa0..00c57f46a3580d37588cfe2c8ace840258b5e805 100644
--- a/matlab/load_m_file_data_legacy.m
+++ b/matlab/load_m_file_data_legacy.m
@@ -97,7 +97,7 @@ fclose(fid);
 % Define a set of variables to be loaded.
 listofvariablestobeloaded = {'b'; 'a'};
 % Test if we can load the data.
-exist('example1.m', 'file'); % To force the detection of the new file under old MATLAB
+exist('example1.m', 'file'); % To force the detection of the new file under old MATLAB. TODO: Test if this trick is still necessary.
 try
     data = load_m_file_data_legacy('example1.m', listofvariablestobeloaded);
     t(1) = 1;
diff --git a/matlab/model_diagnostics.m b/matlab/model_diagnostics.m
index b16cdce7aff7c7f98020e4f9c18947d7459d6d5c..079bb8c4ebdf50e00b9aebe52e2c87dee36a8f9e 100644
--- a/matlab/model_diagnostics.m
+++ b/matlab/model_diagnostics.m
@@ -299,7 +299,7 @@ elseif options_.order >= 2
     [g1, T_order, T] = feval([M_.fname '.sparse.dynamic_g1'], dyn_endo_ss, exo, M_.params, ...
                              dr.ys, M_.dynamic_g1_sparse_rowval, M_.dynamic_g1_sparse_colval, ...
                              M_.dynamic_g1_sparse_colptr);
-    if exist(['+' M_.fname '/+sparse/dynamic_g2.m'],"file") || exist(['+' M_.fname '/+sparse/dynamic_g2.' mexext],"file")
+    if isfile(['+' M_.fname '/+sparse/dynamic_g2.m']) || isfile(['+' M_.fname '/+sparse/dynamic_g2.' mexext])
         g2_v = feval([M_.fname '.sparse.dynamic_g2'], dyn_endo_ss, exo, M_.params, dr.ys, T_order, T);
     end
 end
diff --git a/matlab/moments/UnivariateSpectralDensity.m b/matlab/moments/UnivariateSpectralDensity.m
index d1963e62ac01acb58f514c0057b33ed83ec471e7..18f91de5a735032d8ddbc043ac8246330962e6c0 100644
--- a/matlab/moments/UnivariateSpectralDensity.m
+++ b/matlab/moments/UnivariateSpectralDensity.m
@@ -129,12 +129,12 @@ if isoctave
 end
 
 if ~options_.nograph
-    if ~exist(M_.dname, 'dir')
+    if ~isfolder(M_.dname)
         mkdir('.',M_.dname);
     end
-    if ~exist([M_.dname '/graphs'],'dir')
+    if ~isfolder(sprintf('%s/graphs', M_.dname))
         mkdir(M_.dname,'graphs');
-    end
+        end
 
     for i= 1:nvar
         hh_fig = dyn_figure(options_.nodisplay,'Name',['Spectral Density of ' M_.endo_names{ivar(i)} '.']);
diff --git a/matlab/ols/get_ast.m b/matlab/ols/get_ast.m
index 000c71a1489540d4760f7dbf99490c5ef2229219..8fc095c32cab0f853571c13c1de1ffa9fb40617a 100644
--- a/matlab/ols/get_ast.m
+++ b/matlab/ols/get_ast.m
@@ -39,7 +39,7 @@ end
 
 jsonfile = sprintf('%s%smodel%sjson%smodfile-original.json', M_.fname, filesep(), filesep(), filesep());
 
-if ~exist(jsonfile, 'file')
+if ~isfile(jsonfile)
     error('Could not find %s! Please use the json=compute option (see the Dynare invocation section in the reference manual).', jsonfile)
 end
 
@@ -55,4 +55,4 @@ if ~isempty(eqtags)
     if nargout>1
         jsonmodel = getEquationsByTags(jsonmodel, 'name', eqtags);
     end
-end
\ No newline at end of file
+end
diff --git a/matlab/ols/write_param_init_inc_file.m b/matlab/ols/write_param_init_inc_file.m
index deaa167fca8b1a7338529bcaf39cff65432cd0ef..23454612541e9e04e5db9b4882fec8861fc784e4 100644
--- a/matlab/ols/write_param_init_inc_file.m
+++ b/matlab/ols/write_param_init_inc_file.m
@@ -55,7 +55,7 @@ end
 %% Write file
 % Open
 filepath = [M_.fname filesep 'model' filesep subfolder];
-if ~exist(filepath, 'dir')
+if ~isfolder(filepath)
     mkdir(filepath)
 end
 
diff --git a/matlab/parallel/dynareParallelDelete.m b/matlab/parallel/dynareParallelDelete.m
index 2d4703c3ed2b31636c2493c77e326434276e748c..2d00b3f3557e5c8aed3bf2b80d1d3d12cd531ad3 100644
--- a/matlab/parallel/dynareParallelDelete.m
+++ b/matlab/parallel/dynareParallelDelete.m
@@ -55,7 +55,7 @@ for indPC=1:length(Parallel)
         system(['ssh ',ssh_token,username,Parallel(indPC).ComputerName,' ''/bin/bash --norc -c "rm -f ',directory,pname,fname,'"''']);
     else
         fname_temp=['\\',Parallel(indPC).ComputerName,'\',Parallel(indPC).RemoteDrive,'$\',Parallel(indPC).RemoteDirectory,'\',pname,fname];
-        if exist(fname_temp,'file')
+        if isfile(fname_temp)
             delete(fname_temp);
         end
     end
diff --git a/matlab/partial_information/PCL_Part_info_moments.m b/matlab/partial_information/PCL_Part_info_moments.m
index f4932d3f5efd01bd7098d0949b30de59e78640c8..e1db39b9481692a6100d230824ad49827abebf54 100644
--- a/matlab/partial_information/PCL_Part_info_moments.m
+++ b/matlab/partial_information/PCL_Part_info_moments.m
@@ -28,8 +28,8 @@ function oo_=PCL_Part_info_moments(M_, oo_, options_, varobs, dr, ivar)
 % and the jump variables x(t).
 % The jump variables have dimension NETA
 
-if ~exist([M_.dname '/Output'],'dir')
-    mkdir(M_.dname,'Output');
+if ~isfolder(sprintf('%s%sOutput', M_.dname, filesep))
+    mkdir(M_.dname, 'Output');
 end
 
 warning_old_state = warning;
diff --git a/matlab/perfect-foresight-models/perfect_foresight_with_expectation_errors_setup.m b/matlab/perfect-foresight-models/perfect_foresight_with_expectation_errors_setup.m
index 8074e7bebab1355306ffa3ecf061df148f9df214..2b001224e8eddb337219c44f90bae1ca4d9f7fed 100644
--- a/matlab/perfect-foresight-models/perfect_foresight_with_expectation_errors_setup.m
+++ b/matlab/perfect-foresight-models/perfect_foresight_with_expectation_errors_setup.m
@@ -37,7 +37,7 @@ periods = options_.periods;
 oo_.pfwee.terminal_info = NaN(M_.exo_nbr, periods); % 2nd dimension is informational time
 oo_.pfwee.shocks_info = NaN(M_.exo_nbr, periods, periods); % 2nd dimension is real time, 3rd dimension is informational time
 
-if exist(options_.datafile, 'file')
+if isfile(options_.datafile)
     if ~isempty(M_.det_shocks) || ~isempty(M_.learnt_shocks) || ~isempty(oo_.initial_steady_state) || ~isempty(M_.learnt_endval)
         warning('perfect_foresight_with_expectation_errors_setup: since you passed the datafile option, the contents of shocks and endval blocks will be ignored')
     end
diff --git a/matlab/plot_contributions.m b/matlab/plot_contributions.m
index 74aadda5f30ee6d8e99dcdb7037171ae781281a4..492af14888633dacb3ecac097d8febffb181856b 100644
--- a/matlab/plot_contributions.m
+++ b/matlab/plot_contributions.m
@@ -42,7 +42,7 @@ function plot_contributions(equationname, ds1, ds0)
 global M_
 
 jsonfile = [M_.fname filesep() 'model' filesep() 'json' filesep() 'modfile-original.json'];
-if exist(jsonfile, 'file') ~= 2
+if ~isfile(jsonfile)
     error('Could not find %s! Please use the json option (See the Dynare invocation section in the reference manual).', jsonfile);
 end
 
diff --git a/matlab/print_expectations.m b/matlab/print_expectations.m
index 069dde5076ae7d4c140d20a503d7da1b404a2c19..2871cb52b1e135a217d814061c0b15698b77ec00 100644
--- a/matlab/print_expectations.m
+++ b/matlab/print_expectations.m
@@ -105,7 +105,7 @@ auxmodel = M_.(expectationmodel.auxiliary_model_type).(expectationmodel.auxiliar
 %
 % First print the list of parameters appearing in the VAR_EXPECTATION/PAC_EXPECTATION term.
 %
-if ~exist(sprintf('%s/model/%s', M_.fname, [expectationmodelkind '-expectations']), 'dir')
+if ~isfolder(sprintf('%s/model/%s', M_.fname, [expectationmodelkind '-expectations']))
     mkdir(sprintf('%s/model/%s', M_.fname, [expectationmodelkind '-expectations']))
 end
 
@@ -218,7 +218,7 @@ end
 %
 kind = [expectationmodelkind '_expectations'];
 ndir = sprintf('+%s/+%s/+%s', M_.fname, kind, expectationmodelname);
-if ~exist(ndir, 'dir')
+if ~isfolder(ndir)
     mkdir(sprintf('+%s/+%s/+%s', M_.fname, kind, expectationmodelname));
 end
 filename = sprintf('+%s/+%s/+%s/evaluate.m', M_.fname, kind, expectationmodelname);
@@ -253,7 +253,7 @@ end
 clear('expression');
 
 % Get coefficient values in the target (if any)
-if exist(sprintf('+%s/pac_target_coefficients.m', M_.fname), 'file')
+if isfile(sprintf('+%s/pac_target_coefficients.m', M_.fname))
     targetcoefficients = feval(sprintf('%s.pac_target_coefficients', M_.fname), expectationmodelname, M_.params);
 end
 
diff --git a/matlab/read_variables.m b/matlab/read_variables.m
index 9cc92f8631d1b1b881e20e61869df65ca9cf35a9..f79174db3945971d2f99ff9f28050234b1dc6b92 100644
--- a/matlab/read_variables.m
+++ b/matlab/read_variables.m
@@ -46,25 +46,25 @@ var_size_01 = length(var_names_01);
 
 % Auto-detect extension if not provided
 if isempty(extension)
-    if exist([basename '.m'],'file')
+    if isfile(sprintf('.m', basename))
         extension = '.m';
-    elseif exist([basename '.mat'],'file')
+    elseif isfile(sprintf('.mat', basename))
         extension = '.mat';
-    elseif exist([basename '.xls'],'file')
+    elseif isfile(sprintf('.xls', basename))
         extension = '.xls';
-    elseif exist([basename '.xlsx'],'file')
+    elseif isfile(sprintf('.xlsx', basename))
         extension = '.xlsx';
-    elseif exist([basename '.csv'],'file')
+    elseif isfile(sprintf('.csv', basename))
         extension = '.csv';
     else
-        error(['Can''t find datafile: ' basename '.{m,mat,xls,xlsx,csv}']);
+        error('Can''t find datafile: %s.{m,mat,xls,xlsx,csv}', basename);
     end
 end
 
-fullname = [basename extension];
+fullname = strcat(basename, extension);
 
-if ~exist(fullname)
-    error(['Can''t find datafile: ' fullname ]);
+if ~isfile(fullname)
+    error('Can''t find datafile: %s', fullname)
 end
 
 switch (extension)
diff --git a/matlab/reporting/@page/write.m b/matlab/reporting/@page/write.m
index e51d07d355d7b026384b6a8c0458a9659b616ecd..14889b569952f3fdfd7510c540d2d16c73364695 100644
--- a/matlab/reporting/@page/write.m
+++ b/matlab/reporting/@page/write.m
@@ -49,7 +49,7 @@ fprintf(fid, '\n');
 
 if ~isempty(o.latex)
     dir = [rep_dir '/' o.pageDirName];
-    if exist(dir, 'dir') ~= 7
+    if ~isfolder(dir, 'dir')
         mkdir(dir);
     end
     pagename = [dir '/page_' num2str(pg) '.tex'];
diff --git a/matlab/reporting/@report/compile.m b/matlab/reporting/@report/compile.m
index cab277f52ba60714831acbd46fbe1c0d03f8a051..c984cc0663203ddeacbfc57cf355a0be2c7ccbdd 100644
--- a/matlab/reporting/@report/compile.m
+++ b/matlab/reporting/@report/compile.m
@@ -57,7 +57,7 @@ assert(ischar(opts.compiler), '@report.compile: compiler file must be a string')
 assert(islogical(opts.showReport), '@report.compile: showReport must be either true or false');
 assert(islogical(opts.showOutput), '@report.compile: showOutput must be either true or false');
 
-if exist([o.directory '/' o.fileName], 'file') ~= 2
+if ~isfile([o.directory filesep() o.fileName])
     o.write();
 end
 
diff --git a/matlab/reporting/@report/write.m b/matlab/reporting/@report/write.m
index 39095e9b783a5fce624179a4e90b89350fe80633..ce2ba21ca988033d471b74530926bed409d75cf9 100644
--- a/matlab/reporting/@report/write.m
+++ b/matlab/reporting/@report/write.m
@@ -28,10 +28,10 @@ function o = write(o)
 % You should have received a copy of the GNU General Public License
 % along with Dynare.  If not, see <https://www.gnu.org/licenses/>.
 
-if exist(o.directory, 'dir') ~= 7
+if ~isfolder(o.directory)
     mkdir(o.directory);
 end
-if exist([o.directory '/' o.reportDirName], 'dir') ~= 7
+if ~isfolder([o.directory '/' o.reportDirName])
     mkdir([o.directory '/' o.reportDirName]);
 end
 idx = strfind(o.fileName, '.tex');
diff --git a/matlab/reporting/@report_graph/writeGraphFile.m b/matlab/reporting/@report_graph/writeGraphFile.m
index f405f184c9e7c348aca9a40c56717a919bf7270f..8f747654b190a2caecfd59d68c57cc9c7b016502 100644
--- a/matlab/reporting/@report_graph/writeGraphFile.m
+++ b/matlab/reporting/@report_graph/writeGraphFile.m
@@ -39,7 +39,7 @@ if ne < 1
     return
 end
 
-if exist([rep_dir '/' o.graphDirName], 'dir') ~= 7
+if ~isfolder([rep_dir '/' o.graphDirName])
     mkdir([rep_dir '/' o.graphDirName]);
 end
 if isempty(o.graphName)
diff --git a/matlab/reporting/@report_table/writeTableFile.m b/matlab/reporting/@report_table/writeTableFile.m
index dae86e88d376bebfec9e635aedccb86a63417e62..b97146049f928908879b9ae606edf0a998f10493 100644
--- a/matlab/reporting/@report_table/writeTableFile.m
+++ b/matlab/reporting/@report_table/writeTableFile.m
@@ -42,7 +42,7 @@ end
 if is_data_table
     ne = size(o.table_data{1}.data,2);
 end
-if exist([rep_dir '/' o.tableDirName], 'dir') ~= 7
+if ~isfolder([rep_dir '/' o.tableDirName])
     mkdir([rep_dir '/' o.tableDirName]);
 end
 if isempty(o.tableName)
diff --git a/matlab/rplot.m b/matlab/rplot.m
index 53abb116de3340f7d8e02cac391d27ac64eab8a2..b02ed048195e2aae3451b69ce33144e0e517d665 100644
--- a/matlab/rplot.m
+++ b/matlab/rplot.m
@@ -38,11 +38,11 @@ if isempty(oo_.endo_simul)
 end
 
 % create subdirectory <dname>/graphs if it doesn't exist
-if ~exist(M_.dname, 'dir')
-    mkdir('.',M_.dname);
+if ~isfolder(M_.dname)
+    mkdir('.', M_.dname);
 end
-if ~exist([M_.dname filesep 'graphs'],'dir')
-    mkdir(M_.dname,'graphs');
+if ~isfolder(sprintf('%s%sgraphs', M_.dname, filesep))
+    mkdir(M_.dname, 'graphs');
 end
 
 ix = (1 - M_.maximum_lag:size(oo_.endo_simul,2)-M_.maximum_lag)';
@@ -51,7 +51,7 @@ y = [];
 for k = 1:length(s1)
     if ~any(strcmp(s1{k}, M_.endo_names))
         if ~any(strcmp(s1{k}, M_.exo_names))
-            error ('rplot: One of the variables specified does not exist') ;
+            error ('rplot: One of the variables specified does not exist')
         else
             y = [y; oo_.exo_simul(:, strcmp(s1{k}, M_.exo_names))'] ;
             s1_TeX(k,1)=M_.exo_names_tex(strcmp(s1{k}, M_.exo_names));
diff --git a/matlab/stochastic_solver/simult.m b/matlab/stochastic_solver/simult.m
index 4787d0e6198ff69480b7aed25699dcbdc753e460..c3155bc27a4259b952f9209c6116d6991cce7c5e 100644
--- a/matlab/stochastic_solver/simult.m
+++ b/matlab/stochastic_solver/simult.m
@@ -64,7 +64,7 @@ order = options_.order;
 replic = options_.simul_replic;
 
 if replic > 1
-    if ~exist([M_.dname '/Output'],'dir')
+    if ~isfolder(sprintf('%s%sOutput', M_.dname, filesep))
         mkdir(M_.dname,'Output');
     end
     fname = [M_.dname filesep 'Output' filesep M_.fname,'_simul'];
@@ -96,4 +96,4 @@ end
 
 if replic > 1
     fclose(fh);
-end
\ No newline at end of file
+end
diff --git a/matlab/stochastic_solver/stoch_simul.m b/matlab/stochastic_solver/stoch_simul.m
index 376de52368289594b0e0a1abfa3f4275cd53a234..b19b85e90e5570eb32bfebb2dd37a22e1d5f5dbd 100644
--- a/matlab/stochastic_solver/stoch_simul.m
+++ b/matlab/stochastic_solver/stoch_simul.m
@@ -218,8 +218,8 @@ end
 if options_.irf
     var_listTeX = M_.endo_names_tex(i_var);
     if ~options_.nograph || (TeX && any(strcmp('eps',cellstr(options_.graph_format))))
-        if ~exist([M_.dname '/graphs'],'dir')
-            mkdir(M_.dname,'graphs');
+        if ~isfolder(sprintf('%s%sgraphs', M_.dname, filesep))
+            mkdir(M_.dname, 'graphs');
         end
     end
     if TeX && any(strcmp('eps',cellstr(options_.graph_format)))
diff --git a/matlab/utilities/general/clean_current_folder.m b/matlab/utilities/general/clean_current_folder.m
index f458d6a641a1cc8459e31923ab2e80164f8ab242..4b51afd0590854e4490275666480ee848e366716 100644
--- a/matlab/utilities/general/clean_current_folder.m
+++ b/matlab/utilities/general/clean_current_folder.m
@@ -22,20 +22,20 @@ a = dir('*.mod');
 
 for i = 1:length(a)
     [~,basename] = fileparts(a(i).name);
-    if exist([basename '.m'])
+    if isfile([basename '.m'])
         delete([basename '.m']);
     end
-    if exist([basename '.log'])
+    if isfile([basename '.log'])
         delete([basename '.log']);
     end
-    if exist(basename,'dir')
+    if isfolder(basename)
         rmdir(basename,'s');
     end
-    if exist([basename '_steadystate.m'])
+    if isfolder([basename '_steadystate.m'])
         movefile([basename '_steadystate.m'],['protect_' basename '_steadystate.m']);
     end
     delete([basename '_*'])
-    if exist(['protect_' basename '_steadystate.m'])
+    if isfile(['protect_' basename '_steadystate.m'])
         movefile(['protect_' basename '_steadystate.m'],[basename '_steadystate.m']);
     end
 end
diff --git a/tests/decision_rules/example1_use_dll.mod b/tests/decision_rules/example1_use_dll.mod
index 8c5b200d6d5df10d307ec946af233bb0fda34b59..124ac6dbc4db20790cddba30ef3a9180d38a6106 100644
--- a/tests/decision_rules/example1_use_dll.mod
+++ b/tests/decision_rules/example1_use_dll.mod
@@ -44,30 +44,34 @@ end;
 
 stoch_simul(nograph);
 
-if ~exist(['example1' filesep 'Output' filesep 'example1_results.mat'],'file');
-   error('example1 must be run first');
-end;
+matfilename = sprintf('example1%sOutput%sexample1_results.mat', filesep, filesep);
+
+if ~isfile(matfilename)
+   error('example1 must be run first')
+end
+
+[p, f, e] = fileparts(matfilename);
 
-oo1 = load(['example1' filesep 'Output' filesep 'example1_results'],'oo_');
+oo1 = load(fullfile(p, f),'oo_');
 
 dr0 = oo1.oo_.dr;
 dr = oo_.dr;
 
-if max(max(abs(dr0.ghx - dr.ghx))) > 1e-12;
-   error('error in ghx');
-end;
-if max(max(abs(dr0.ghu - dr.ghu))) > 1e-12;
-   error('error in ghu');
-end;
-if max(max(abs(dr0.ghxx - dr.ghxx))) > 1e-12;
-   error('error in ghxx');
-end;
-if max(max(abs(dr0.ghuu - dr.ghuu))) > 1e-12;
-   error('error in ghuu');
-end;
-if max(max(abs(dr0.ghxu - dr.ghxu))) > 1e-12;
-   error('error in ghxu');
-end;
-if max(max(abs(dr0.ghs2 - dr.ghs2))) > 1e-12;
-   error('error in ghs2');
-end;
+if max(max(abs(dr0.ghx - dr.ghx))) > 1e-12
+   error('error in ghx')
+end
+if max(max(abs(dr0.ghu - dr.ghu))) > 1e-12
+   error('error in ghu')
+end
+if max(max(abs(dr0.ghxx - dr.ghxx))) > 1e-12
+   error('error in ghxx')
+end
+if max(max(abs(dr0.ghuu - dr.ghuu))) > 1e-12
+   error('error in ghuu')
+end
+if max(max(abs(dr0.ghxu - dr.ghxu))) > 1e-12
+   error('error in ghxu')
+end
+if max(max(abs(dr0.ghs2 - dr.ghs2))) > 1e-12
+   error('error in ghs2')
+end
diff --git a/tests/decision_rules/k_order/fs2000k2_m.mod b/tests/decision_rules/k_order/fs2000k2_m.mod
index 02ca4545081bdb5519f4096c4c2beb1a23a371ab..f5383d7b66922da497a34edb60e52a9c6546a841 100644
--- a/tests/decision_rules/k_order/fs2000k2_m.mod
+++ b/tests/decision_rules/k_order/fs2000k2_m.mod
@@ -66,31 +66,34 @@ steady;
 
 stoch_simul(order=2,k_order_solver,periods=1000);
 
-if ~exist(['fs2000k2a' filesep 'Output' filesep 'fs2000k2a_results.mat'],'file');
-   error('fs2000k2a must be run first');
+matfilename = sprintf('fs2000k2a%sOutput%sfs2000k2a_results.mat', filesep, filesep);
+
+if ~isfile(matfilename)
+   error('fs2000k2a must be run first')
 end;
 
-oo1 = load(['fs2000k2a' filesep 'Output' filesep 'fs2000k2a_results'],'oo_');
+[p, f, e] = fileparts(matfilename);
+
+oo1 = load(fullfile(p, f), 'oo_');
 
 dr0 = oo1.oo_.dr;
 dr = oo_.dr;
 
-if max(max(abs(dr0.ghx - dr.ghx))) > 1e-12;
-   error('error in ghx');
-end;
-if max(max(abs(dr0.ghu - dr.ghu))) > 1e-12;
-   error('error in ghu');
-end;
-if max(max(abs(dr0.ghxx - dr.ghxx))) > 1e-12;
-   error('error in ghxx');
-end;
-if max(max(abs(dr0.ghuu - dr.ghuu))) > 1e-12;
-   error('error in ghuu');
-end;
-if max(max(abs(dr0.ghxu - dr.ghxu))) > 1e-12;
-   error('error in ghxu');
-end;
-if max(max(abs(dr0.ghs2 - dr.ghs2))) > 1e-12;
-   error('error in ghs2');
-end;
-
+if max(max(abs(dr0.ghx - dr.ghx))) > 1e-12
+   error('error in ghx')
+end
+if max(max(abs(dr0.ghu - dr.ghu))) > 1e-12
+   error('error in ghu')
+end
+if max(max(abs(dr0.ghxx - dr.ghxx))) > 1e-12
+   error('error in ghxx')
+end
+if max(max(abs(dr0.ghuu - dr.ghuu))) > 1e-12
+   error('error in ghuu')
+end
+if max(max(abs(dr0.ghxu - dr.ghxu))) > 1e-12
+   error('error in ghxu')
+end
+if max(max(abs(dr0.ghs2 - dr.ghs2))) > 1e-12
+   error('error in ghs2')
+end
diff --git a/tests/decision_rules/k_order/fs2000k2_use_dll.mod b/tests/decision_rules/k_order/fs2000k2_use_dll.mod
index 9b5ea9dd9553646b66db78af28fade1738a0ae91..2a56cb73bf362a0f202d6ac3e0ce516393aaa260 100644
--- a/tests/decision_rules/k_order/fs2000k2_use_dll.mod
+++ b/tests/decision_rules/k_order/fs2000k2_use_dll.mod
@@ -66,31 +66,34 @@ steady;
 
 stoch_simul(order=2,k_order_solver,periods=1000);
 
-if ~exist(['fs2000k2a' filesep 'Output' filesep 'fs2000k2a_results.mat'],'file');
-   error('fs2000k2a must be run first');
-end;
+matfilename = sprintf('fs2000k2a%sOutput%sfs2000k2a_results.mat', filesep, filesep);
+
+if ~isfile(matfilename)
+   error('fs2000k2a must be run first')
+end
+
+[p, f, e] = fileparts(matfilename);
 
-oo1 = load(['fs2000k2a' filesep 'Output' filesep 'fs2000k2a_results'],'oo_');
+oo1 = load(fullfile(p, f), 'oo_');
 
 dr0 = oo1.oo_.dr;
 dr = oo_.dr;
 
-if max(max(abs(dr0.ghx - dr.ghx))) > 1e-12;
-   error('error in ghx');
-end;
-if max(max(abs(dr0.ghu - dr.ghu))) > 1e-12;
-   error('error in ghu');
-end;
-if max(max(abs(dr0.ghxx - dr.ghxx))) > 1e-12;
-   error('error in ghxx');
-end;
-if max(max(abs(dr0.ghuu - dr.ghuu))) > 1e-12;
-   error('error in ghuu');
-end;
-if max(max(abs(dr0.ghxu - dr.ghxu))) > 1e-12;
-   error('error in ghxu');
-end;
-if max(max(abs(dr0.ghs2 - dr.ghs2))) > 1e-12;
-   error('error in ghs2');
-end;
-
+if max(max(abs(dr0.ghx - dr.ghx))) > 1e-12
+   error('error in ghx')
+end
+if max(max(abs(dr0.ghu - dr.ghu))) > 1e-12
+   error('error in ghu')
+end
+if max(max(abs(dr0.ghxx - dr.ghxx))) > 1e-12
+   error('error in ghxx')
+end
+if max(max(abs(dr0.ghuu - dr.ghuu))) > 1e-12
+   error('error in ghuu')
+end
+if max(max(abs(dr0.ghxu - dr.ghxu))) > 1e-12
+   error('error in ghxu')
+end
+if max(max(abs(dr0.ghs2 - dr.ghs2))) > 1e-12
+   error('error in ghs2')
+end
diff --git a/tests/decision_rules/k_order/fs2000k3_m.mod b/tests/decision_rules/k_order/fs2000k3_m.mod
index fa91efec8d601ae1338c7ac6f95e148c1ac0323d..ee4f68a943f0e3c615949e8761f93180006f83ed 100644
--- a/tests/decision_rules/k_order/fs2000k3_m.mod
+++ b/tests/decision_rules/k_order/fs2000k3_m.mod
@@ -66,25 +66,28 @@ steady;
 
 stoch_simul(order=3,periods=1000);
 
-if ~exist(['fs2000k2a' filesep 'Output' filesep 'fs2000k2a_results.mat'],'file');
-   error('fs2000k2a must be run first');
-end;
+matfilename = sprintf('fs2000k2a%sOutput%sfs2000k2a_results.mat', filesep, filesep);
+
+if ~isfile(matfilename)
+   error('fs2000k2a must be run first')
+end
 
-oo1 = load(['fs2000k2a' filesep 'Output' filesep 'fs2000k2a_results'],'oo_');
+[p, f, e] = fileparts(matfilename);
+
+oo1 = load(fullfile(p, f), 'oo_');
 
 dr0 = oo1.oo_.dr;
 dr = oo_.dr;
 
-if max(max(abs(dr0.ghxx - dr.ghxx))) > 1e-12;
-   error('error in ghxx');
-end;
-if max(max(abs(dr0.ghuu - dr.ghuu))) > 1e-12;
-   error('error in ghuu');
-end;
-if max(max(abs(dr0.ghxu - dr.ghxu))) > 1e-12;
-   error('error in ghxu');
-end;
-if max(max(abs(dr0.ghs2 - dr.ghs2))) > 1e-12;
-   error('error in ghs2');
-end;
-
+if max(max(abs(dr0.ghxx - dr.ghxx))) > 1e-12
+   error('error in ghxx')
+end
+if max(max(abs(dr0.ghuu - dr.ghuu))) > 1e-12
+   error('error in ghuu')
+end
+if max(max(abs(dr0.ghxu - dr.ghxu))) > 1e-12
+   error('error in ghxu')
+end
+if max(max(abs(dr0.ghs2 - dr.ghs2))) > 1e-12
+   error('error in ghs2')
+end
diff --git a/tests/decision_rules/k_order/fs2000k3_use_dll.mod b/tests/decision_rules/k_order/fs2000k3_use_dll.mod
index 2028443b60337d372b399a22c3bc81ca12b07ff6..b113de436f88627edf8564bef0df7036eb018727 100644
--- a/tests/decision_rules/k_order/fs2000k3_use_dll.mod
+++ b/tests/decision_rules/k_order/fs2000k3_use_dll.mod
@@ -66,25 +66,29 @@ steady;
 
 stoch_simul(order=3,periods=1000);
 
-if ~exist(['fs2000k2a' filesep 'Output' filesep 'fs2000k2a_results.mat'],'file');
-   error('fs2000k2a must be run first');
-end;
 
-oo1 = load(['fs2000k2a' filesep 'Output' filesep 'fs2000k2a_results'],'oo_');
+matfilename = sprintf('fs2000k2a%sOutput%sfs2000k2a_results.mat', filesep, filesep);
+
+if ~isfile(matfilename)
+   error('fs2000k2a must be run first')
+end
+
+[p, f, e] = fileparts(matfilename);
+
+oo1 = load(fullfile(p, f), 'oo_');
 
 dr0 = oo1.oo_.dr;
 dr = oo_.dr;
 
-if max(max(abs(dr0.ghxx - dr.ghxx))) > 1e-12;
-   error('error in ghxx');
-end;
-if max(max(abs(dr0.ghuu - dr.ghuu))) > 1e-12;
-   error('error in ghuu');
-end;
-if max(max(abs(dr0.ghxu - dr.ghxu))) > 1e-12;
-   error('error in ghxu');
-end;
-if max(max(abs(dr0.ghs2 - dr.ghs2))) > 1e-12;
-   error('error in ghs2');
-end;
-
+if max(max(abs(dr0.ghxx - dr.ghxx))) > 1e-12
+   error('error in ghxx')
+end
+if max(max(abs(dr0.ghuu - dr.ghuu))) > 1e-12
+   error('error in ghuu')
+end
+if max(max(abs(dr0.ghxu - dr.ghxu))) > 1e-12
+   error('error in ghxu')
+end
+if max(max(abs(dr0.ghs2 - dr.ghs2))) > 1e-12
+   error('error in ghs2')
+end
diff --git a/tests/decision_rules/k_order/fs2000k_1_m.mod b/tests/decision_rules/k_order/fs2000k_1_m.mod
index f4bad22ce1300e718ead044c48d1bf4c806403e2..7f267257b7c485c95ddcd18905b62e7ed0ad9eaa 100644
--- a/tests/decision_rules/k_order/fs2000k_1_m.mod
+++ b/tests/decision_rules/k_order/fs2000k_1_m.mod
@@ -74,11 +74,15 @@ steady;
 
 stoch_simul(order=2,k_order_solver,irf=0);
 
-if ~exist(['fs2000k2_m' filesep 'Output' filesep 'fs2000k2_m_results.mat'],'file');
-   error('fs2000k2_m must be run first');
-end;
+matfilename = sprintf('fs2000k2_m%sOutput%sfs2000k2_m_results.mat', filesep, filesep);
+
+if ~isfile(matfilename)
+   error('fs2000k2_m must be run first')
+end
+
+[p, f, e] = fileparts(matfilename);
 
-oo1 = load(['fs2000k2_m' filesep 'Output' filesep 'fs2000k2_m_results'],'oo_');
+oo1 = load(fullfile(p, f), 'oo_');
 
 dr0 = oo1.oo_.dr;
 dr = oo_.dr;
@@ -88,22 +92,21 @@ ikc = [1 3 4 2];
 ikc2 = [1 3 4 2 9 11 12 10 13 15 16 14 5 7 8 6];
 ikc2u = [1 2 5 6 7 8 3 4];
 
-if max(max(abs(dr0.ghx - dr.ghx(ikr,ikc)))) > 1e-12;
-   error('error in ghx');
-end;
-if max(max(abs(dr0.ghu - dr.ghu(ikr,:)))) > 1e-12;
-   error('error in ghu');
-end;
-if max(max(abs(dr0.ghxx - dr.ghxx(ikr,ikc2)))) > 1e-12;
-   error('error in ghxx');
-end;
-if max(max(abs(dr0.ghuu - dr.ghuu(ikr,:)))) > 1e-12;
-   error('error in ghuu');
-end;
-if max(max(abs(dr0.ghxu - dr.ghxu(ikr,ikc2u)))) > 1e-12;
-   error('error in ghxu');
-end;
-if max(max(abs(dr0.ghs2 - dr.ghs2(ikr,:)))) > 1e-12;
-   error('error in ghs2');
-end;
-
+if max(max(abs(dr0.ghx - dr.ghx(ikr,ikc)))) > 1e-12
+   error('error in ghx')
+end
+if max(max(abs(dr0.ghu - dr.ghu(ikr,:)))) > 1e-12
+   error('error in ghu')
+end
+if max(max(abs(dr0.ghxx - dr.ghxx(ikr,ikc2)))) > 1e-12
+   error('error in ghxx')
+end
+if max(max(abs(dr0.ghuu - dr.ghuu(ikr,:)))) > 1e-12
+   error('error in ghuu')
+end
+if max(max(abs(dr0.ghxu - dr.ghxu(ikr,ikc2u)))) > 1e-12
+   error('error in ghxu')
+end
+if max(max(abs(dr0.ghs2 - dr.ghs2(ikr,:)))) > 1e-12
+   error('error in ghs2')
+end
diff --git a/tests/decision_rules/k_order/fs2000k_1_use_dll.mod b/tests/decision_rules/k_order/fs2000k_1_use_dll.mod
index f4eb0300810d3c83a13fe5c8a55ea5dc8add1e73..e6ad4371f45dbc6fccd8ddf3c0867dbee290c284 100644
--- a/tests/decision_rules/k_order/fs2000k_1_use_dll.mod
+++ b/tests/decision_rules/k_order/fs2000k_1_use_dll.mod
@@ -73,11 +73,15 @@ steady;
 
 stoch_simul(order=2,k_order_solver,irf=0);
 
-if ~exist(['fs2000k2_use_dll' filesep 'Output' filesep 'fs2000k2_use_dll_results.mat'],'file');
-   error('fs2000k2_use_dll must be run first');
-end;
+matfilename = sprintf('fs2000k2_use_dll%sOutput%sfs2000k2_use_dll_results.mat', filesep, filesep);
+
+if ~isfile(matfilename)
+   error('fs2000k2_use_dll must be run first')
+end
+
+[p, f, e] = fileparts(matfilename);
 
-oo1 = load(['fs2000k2_use_dll' filesep 'Output' filesep 'fs2000k2_use_dll_results'],'oo_');
+oo1 = load(fullfile(p, f), 'oo_');
 
 dr0 = oo1.oo_.dr;
 dr = oo_.dr;
@@ -87,22 +91,21 @@ ikc = [1 3 4 2];
 ikc2 = [1 3 4 2 9 11 12 10 13 15 16 14 5 7 8 6];
 ikc2u = [1 2 5 6 7 8 3 4];
 
-if max(max(abs(dr0.ghx - dr.ghx(ikr,ikc)))) > 1e-12;
-   error('error in ghx');
-end;
-if max(max(abs(dr0.ghu - dr.ghu(ikr,:)))) > 1e-12;
-   error('error in ghu');
-end;
-if max(max(abs(dr0.ghxx - dr.ghxx(ikr,ikc2)))) > 1e-12;
-   error('error in ghxx');
-end;
-if max(max(abs(dr0.ghuu - dr.ghuu(ikr,:)))) > 1e-12;
-   error('error in ghuu');
-end;
-if max(max(abs(dr0.ghxu - dr.ghxu(ikr,ikc2u)))) > 1e-12;
-   error('error in ghxu');
-end;
-if max(max(abs(dr0.ghs2 - dr.ghs2(ikr,:)))) > 1e-12;
-   error('error in ghs2');
-end;
-
+if max(max(abs(dr0.ghx - dr.ghx(ikr,ikc)))) > 1e-12
+   error('error in ghx')
+end
+if max(max(abs(dr0.ghu - dr.ghu(ikr,:)))) > 1e-12
+   error('error in ghu')
+end
+if max(max(abs(dr0.ghxx - dr.ghxx(ikr,ikc2)))) > 1e-12
+   error('error in ghxx')
+end
+if max(max(abs(dr0.ghuu - dr.ghuu(ikr,:)))) > 1e-12
+   error('error in ghuu')
+end
+if max(max(abs(dr0.ghxu - dr.ghxu(ikr,ikc2u)))) > 1e-12
+   error('error in ghxu')
+end
+if max(max(abs(dr0.ghs2 - dr.ghs2(ikr,:)))) > 1e-12
+   error('error in ghs2')
+end
diff --git a/tests/dynare-command-options/ramst.mod b/tests/dynare-command-options/ramst.mod
index 1f3da08e86afb3f5c089d08bd8d081d0828d5fe6..bde08b641e03583d0e7a66362c843bd1e81b4b11 100644
--- a/tests/dynare-command-options/ramst.mod
+++ b/tests/dynare-command-options/ramst.mod
@@ -38,6 +38,6 @@ perfect_foresight_solver;
 rplot c;
 rplot k;
 
-if ~exist('./ramst/model/json/modfile.json', 'file') || exist('./ramst.log', 'file')
+if ~isfile('./ramst/model/json/modfile.json') || isfile('./ramst.log')
    error('The dynare command did not honor the options provided in the mod file!')
 end
diff --git a/tests/estimation/fs2000.mod b/tests/estimation/fs2000.mod
index 071d047b65d62e005d54b14b0d31b28de54e2f8e..e2e346684137189c55eeca3fd5814cc601c83928 100644
--- a/tests/estimation/fs2000.mod
+++ b/tests/estimation/fs2000.mod
@@ -119,8 +119,8 @@ if ~isoctave
         error('Adding draws did not result in the same chain')
     end
 end
-        
-if ~exist([M_.dname filesep 'Output'],'dir')
+
+if ~isfolder(sprintf('%s%sOutput', M_.dname, filesep))
     mkdir(M_.dname,'Output');
 end
 save([M_.dname filesep 'Output' filesep 'fs2000_results.mat'], 'oo_');
diff --git a/tests/optimal_policy/neo_growth_ramsey.mod b/tests/optimal_policy/neo_growth_ramsey.mod
index b766c315363b446ffcd2c2785ac02fbb5e15e825..14c50b116cc6336d0d3409fdd75cf3d8c6bc44e3 100644
--- a/tests/optimal_policy/neo_growth_ramsey.mod
+++ b/tests/optimal_policy/neo_growth_ramsey.mod
@@ -33,34 +33,39 @@ stoch_simul(order=2, irf=0);
 
 planner_objective_value = evaluate_planner_objective(M_, options_, oo_);
 
-if ~exist(['neo_growth' filesep 'Output' filesep 'neo_growth_results.mat'],'file');
-   error('neo_growth must be run first');
-end;
+matfilename = sprintf('neo_growth%sOutput%sneo_growth_results.mat', filesep, filesep);
+
+if ~isfile(matfilename)
+   error('neo_growth must be run first')
+end
 
-oo1 = load(['neo_growth' filesep 'Output' filesep 'neo_growth_results'],'oo_');
-M1 = load(['neo_growth' filesep 'Output' filesep 'neo_growth_results'],'M_');
-options1 = load(['neo_growth' filesep 'Output' filesep 'neo_growth_results'],'options_');
-unc_W_hand = oo1.oo_.mean(strmatch('W',M1.M_.endo_names,'exact'));
+[p, f, e] = fileparts(matfilename);
+matfilebasename = fullfile(p, f);
+
+oo1 = load(matfilebasename,'oo_');
+M1 = load(matfilebasename,'M_');
+options1 = load(matfilebasename,'options_');
+unc_W_hand = oo1.oo_.mean(strmatch('W', M1.M_.endo_names,'exact'));
 
 initial_condition_states = repmat(oo1.oo_.dr.ys,1,M1.M_.maximum_lag);
 shock_matrix = zeros(1,M1.M_.exo_nbr);
 y_sim = simult_(M1.M_,options1.options_,initial_condition_states,oo1.oo_.dr,shock_matrix,options1.options_.order);
 cond_W_hand_L_SS=y_sim(strmatch('W',M1.M_.endo_names,'exact'),2);
 
-if abs((unc_W_hand - planner_objective_value.unconditional)/unc_W_hand) > 1e-6;
-   error('Inaccurate unconditional welfare assessment');
-end;
-if abs(cond_W_hand_L_SS - planner_objective_value.conditional.steady_initial_multiplier) > 1e-6;
-   error('Inaccurate conditional welfare with Lagrange multiplier set to its steady-state value');
-end;
+if abs((unc_W_hand - planner_objective_value.unconditional)/unc_W_hand) > 1e-6
+   error('Inaccurate unconditional welfare assessment')
+end
+if abs(cond_W_hand_L_SS - planner_objective_value.conditional.steady_initial_multiplier) > 1e-6
+   error('Inaccurate conditional welfare with Lagrange multiplier set to its steady-state value')
+end
 
 if abs(oo_.mean(strmatch('U',M_.endo_names,'exact'))-oo1.oo_.mean(strmatch('U',M1.M_.endo_names,'exact')))>1e-6
-    error('Utility inconsistent');
+    error('Utility inconsistent')
 end
 
-if abs(planner_objective_value.unconditional-oo_.mean(strmatch('U',M_.endo_names,'exact'))/(1-beta)) > 1e-6;
-   error('Unconditional welfare assessment does not match utility');
-end;
+if abs(planner_objective_value.unconditional-oo_.mean(strmatch('U',M_.endo_names,'exact'))/(1-beta)) > 1e-6
+   error('Unconditional welfare assessment does not match utility')
+end
 
 
 initial_condition_states = zeros(M1.M_.endo_nbr,M1.M_.maximum_lag);
@@ -68,6 +73,6 @@ initial_condition_states(1:M1.M_.orig_endo_nbr,:) = repmat(oo1.oo_.dr.ys(1:M1.M_
 shock_matrix = zeros(1,M1.M_.exo_nbr);
 y_sim = simult_(M1.M_,options1.options_,initial_condition_states,oo1.oo_.dr,shock_matrix,options1.options_.order);
 cond_W_hand_L_0=y_sim(strmatch('W',M1.M_.endo_names,'exact'),2);
-if abs(cond_W_hand_L_0 - planner_objective_value.conditional.zero_initial_multiplier) > 1e-6;
-   error('Inaccurate conditional welfare with zero Lagrange multiplier');
+if abs(cond_W_hand_L_0 - planner_objective_value.conditional.zero_initial_multiplier) > 1e-6
+   error('Inaccurate conditional welfare with zero Lagrange multiplier')
 end;
diff --git a/tests/optimal_policy/neo_growth_ramsey_foresight.mod b/tests/optimal_policy/neo_growth_ramsey_foresight.mod
index 1b87f9844dac0a86ab7e0d14deaad8966be8426a..b2833612f1d09086472f374c5e7bd408e686b1b1 100644
--- a/tests/optimal_policy/neo_growth_ramsey_foresight.mod
+++ b/tests/optimal_policy/neo_growth_ramsey_foresight.mod
@@ -35,19 +35,24 @@ perfect_foresight_solver;
 
 planner_objective_value = evaluate_planner_objective(M_, options_, oo_);
 
-if ~exist('neo_growth_foresight_results.mat','file');
-   error('neo_growth_foresight must be run first');
+matfilename = sprintf('neo_growth_foresight%sOutput%sneo_growth_foresight_results.mat', filesep, filesep);
+
+if ~isfile(matfilename)
+   error('neo_growth_foresight must be run first')
 end;
 
-oo1 = load(['neo_growth_foresight' filesep 'Output' filesep 'neo_growth_foresight_results'],'oo_');
-M1 = load(['neo_growth_foresight' filesep 'Output' filesep 'neo_growth_foresight_results'],'M_');
-options1 = load(['neo_growth_foresight' filesep 'Output' filesep 'neo_growth_foresight_results'],'options_');
+[p, f, e] = fileparts(matfilename);
+matfilebasename = fullfile(p, f);
+
+oo1 = load(matfilebasename, 'oo_');
+M1 = load(matfilebasename, 'M_');
+options1 = load(matfilebasename, 'options_');
 cond_W_hand = oo1.oo_.endo_simul(strmatch('W',M1.M_.endo_names,'exact'),2);
 unc_W_hand = oo1.oo_.endo_simul(strmatch('W',M1.M_.endo_names,'exact'),end);
 
-if abs((unc_W_hand - planner_objective_value.unconditional)/unc_W_hand) > 1e-6;
-   error('Inaccurate unconditional welfare assessment');
-end;
-if abs((cond_W_hand - planner_objective_value.conditional)/cond_W_hand) > 1e-6;
-   error('Inaccurate conditional welfare assessment');
-end;
+if abs((unc_W_hand - planner_objective_value.unconditional)/unc_W_hand) > 1e-6
+   error('Inaccurate unconditional welfare assessment')
+end
+if abs((cond_W_hand - planner_objective_value.conditional)/cond_W_hand) > 1e-6
+   error('Inaccurate conditional welfare assessment')
+end
diff --git a/tests/optimal_policy/neo_growth_ramsey_k_order.mod b/tests/optimal_policy/neo_growth_ramsey_k_order.mod
index e0aa648f9d0d2e2e0334e967c5756b9b7e8147b3..06a33d9f1c30097b7a95011b100c34cedcef070d 100644
--- a/tests/optimal_policy/neo_growth_ramsey_k_order.mod
+++ b/tests/optimal_policy/neo_growth_ramsey_k_order.mod
@@ -35,15 +35,20 @@ evaluate_planner_objective(periods=10000,drop=1000);
 
 [W_dynpp] = k_order_welfare(oo_.dr, M_, options_);
 
-if ~exist(['neo_growth_k_order' filesep 'Output' filesep 'neo_growth_k_order_results.mat'],'file');
-   error('neo_growth_k_order must be run first');
-end;
+matfilename = sprintf('neo_growth_k_order%sOutput%sneo_growth_k_order_results.mat', filesep, filesep);
+
+if ~isfile(matfilename)
+   error('neo_growth_k_order must be run first')
+end
 
-oo = load(['neo_growth_k_order' filesep 'Output' filesep 'neo_growth_k_order_results'],'oo_');
-M = load(['neo_growth_k_order' filesep 'Output' filesep 'neo_growth_k_order_results'],'M_');
-options = load(['neo_growth_k_order' filesep 'Output' filesep 'neo_growth_k_order_results'],'options_');
+[p, f, e] = fileparts(matfilename);
+matfilebasename = fullfile(p, f);
 
-ind_W = strmatch('W', M.M_.endo_names,'exact');
+oo = load(matfilebasename, 'oo_');
+M = load(matfilebasename, 'M_');
+options = load(matfilebasename, 'options_');
+
+ind_W = strmatch('W', M.M_.endo_names, 'exact');
 
 err = -1e6;
 for i = 1:options_.order
@@ -53,6 +58,6 @@ for i = 1:options_.order
    err = max(err, tmp_err);
 end
 
-if err > 1e-10;
-   error('Inaccurate assessment of the derivatives of the welfare function');
-end;
+if err > 1e-10
+   error('Inaccurate assessment of the derivatives of the welfare function')
+end
diff --git a/tests/run_block_bytecode_tests.m b/tests/run_block_bytecode_tests.m
index 0c95b794ab539939b7c58eefbb4d0d51b730c674..21f7058cae4a94a41df6e7bc9667ed87fd5071e5 100644
--- a/tests/run_block_bytecode_tests.m
+++ b/tests/run_block_bytecode_tests.m
@@ -56,7 +56,7 @@ for blockFlag = 0:1
 
         % Workaround for strange race condition related to the static/dynamic
         % files (especially when we switch to/from use_dll)
-        if isoctave && exist('+ls2003_tmp')
+        if isoctave && isfolder('+ls2003_tmp')
             rmdir('+ls2003_tmp', 's');
             pause(1)
         end
diff --git a/tests/stochastic_simulations/example2long.mod b/tests/stochastic_simulations/example2long.mod
index f68bd608a5d04b9e42626cf2fb485fb14deb3286..0c273a96a898375c7af1fcef0020f4fe4f3f9bc4 100644
--- a/tests/stochastic_simulations/example2long.mod
+++ b/tests/stochastic_simulations/example2long.mod
@@ -51,11 +51,15 @@ end;
 
 stoch_simul;
 
-if ~exist(['example1long' filesep 'Output' filesep 'example1long_results.mat'],'file');
+matfilename = sprintf('example1long%sOutput%sexample1long_results.mat', filesep, filesep);
+
+if ~isfile(matfilename);
    error('example1long must be run first');
 end;
 
-oo1 = load(['example1long' filesep 'Output' filesep 'example1long_results'],'oo_');
+[p, f, e] = fileparts(matfilename);
+
+oo1 = load(fullfile(p, f), 'oo_');
 
 dr0 = oo1.oo_.dr;
 dr = oo_.dr;
@@ -100,4 +104,4 @@ end
 
 if d_ghs2>epsilon
    error('error in ghs2')
-end
\ No newline at end of file
+end
diff --git a/tests/stochastic_simulations/example2long_use_dll.mod b/tests/stochastic_simulations/example2long_use_dll.mod
index 0dab99e81d29579a3da563aa89198f458057ac90..eda50bd03f7ee64241f74e56b5b46f8449d6d895 100644
--- a/tests/stochastic_simulations/example2long_use_dll.mod
+++ b/tests/stochastic_simulations/example2long_use_dll.mod
@@ -51,11 +51,15 @@ end;
 
 stoch_simul;
 
-if ~exist(['example1long' filesep 'Output' filesep 'example1long_results.mat'],'file');
+matfilename = sprintf('example1long%sOutput%sexample1long_results.mat', filesep, filesep);
+
+if ~isfile(matfilename);
    error('example1long must be run first');
 end;
 
-oo1 = load(['example1long' filesep 'Output' filesep 'example1long_results'],'oo_');
+[p, f, e] = fileparts(matfilename);
+
+oo1 = load(fullname(p, f), 'oo_');
 
 
 dr0 = oo1.oo_.dr;
@@ -101,4 +105,4 @@ end
 
 if d_ghs2>epsilon
    error('error in ghs2')
-end
\ No newline at end of file
+end