From 1391c4efb374e5d9df2ccf8cb0fa56ec3f10976b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Tue, 5 Jun 2018 19:43:43 +0200
Subject: [PATCH] Fix user_has_octave_forge_package

Packages are no longer autoloaded, so testing whether they are "Loaded" does
not work in all cases.

The function now ensures that the package is loaded.

(cherry picked from commit 7cbac0c989f6aa4863fca7a6dc9b06a1e2f74578)
---
 matlab/optimization/dynare_minimize_objective.m | 6 +-----
 matlab/user_has_octave_forge_package.m          | 9 ++++++++-
 2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/matlab/optimization/dynare_minimize_objective.m b/matlab/optimization/dynare_minimize_objective.m
index 025d41189..b51617010 100644
--- a/matlab/optimization/dynare_minimize_objective.m
+++ b/matlab/optimization/dynare_minimize_objective.m
@@ -135,11 +135,7 @@ switch minimizer_algorithm
         simulated_annealing(objective_function,start_par_value,sa_options,LB,UB,varargin{:});
   case 3
     if isoctave && ~user_has_octave_forge_package('optim')
-        try
-            pkg load optim
-        catch
-            error('Optimization algorithm 3 requires the optim package')
-        end
+        error('Optimization algorithm 3 requires the optim package')
     elseif ~isoctave && ~user_has_matlab_license('optimization_toolbox')
         error('Optimization algorithm 3 requires the Optimization Toolbox')
     end
diff --git a/matlab/user_has_octave_forge_package.m b/matlab/user_has_octave_forge_package.m
index 07d918d42..a3f1bc38d 100644
--- a/matlab/user_has_octave_forge_package.m
+++ b/matlab/user_has_octave_forge_package.m
@@ -20,4 +20,11 @@ function [hasPackage] = user_has_octave_forge_package(package)
 
 [desc,flag] = pkg('describe', package);
 
-hasPackage = isequal(flag{1,1}, 'Loaded');
+if isequal(flag{1,1}, 'Not installed')
+    hasPackage = 0;
+else
+    if isequal(flag{1,1}, 'Not loaded')
+        pkg('load', package);
+    end
+    hasPackage = 1;
+end
-- 
GitLab