# Question: Dynare in a loop over a parameter space

**URL:** <https://forum.dynare.org/t/question-dynare-in-a-loop-over-a-parameter-space/5398>\
**Category:** Dynare help (legacy posts)\
**Created:** [1 April 2016 14:48 UTC](https://forum.dynare.org/t/question-dynare-in-a-loop-over-a-parameter-space/5398 "2016-04-01T14:48:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Econbee](https://forum.dynare.org/letter_avatar_proxy/v4/letter/e/c57346/32.png) [@Econbee](https://forum.dynare.org/u/Econbee)\
**Post date:** [1 April 2016 14:48 UTC](https://forum.dynare.org/t/question-dynare-in-a-loop-over-a-parameter-space/5398/1 "2016-04-01T14:48:36Z")

</div>

Dear Johannes,

I am trying to explore a parameter space, at each point of which I would simulate a data set. At each point, I would use Dynare to solve and simulate. The functions included the three ones in the attachment. When I run test.m, I got the following error message:

Attempt to reference field of non-structure array.  
Error in set\_param\_value (line 22)  
i = strmatch(pname,M\_.param\_names,‘exact’);  
Error in test (line 11)  
set\_param\_value(‘gamma\_R\_PI’,gamma\_MP(i));

Could you please help me check what the problem is?

Thank you very much!  
[test\_steadystate.m](https://forum.dynare.org/uploads/default/original/2X/9/90e8abae834a57ca2b6f0a5677adfe701c8ccd4a.m) (3.51 KB)  
[test.mod](https://forum.dynare.org/uploads/default/original/2X/e/e1f04b02d6ccd2839aa998f8ebcd6aebb259ec18.mod) (12.1 KB)  
[test.m](https://forum.dynare.org/uploads/default/original/2X/c/cb49540a2ff4f44cbafcd4ce63bf1dd0645420e7.m) (321 Bytes)

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [2 April 2016 07:53 UTC](https://forum.dynare.org/t/question-dynare-in-a-loop-over-a-parameter-space/5398/2 "2016-04-02T07:53:42Z")

</div>

Please find attached the final version. The main file is

```auto
%run once to set up model and workspace
dynare test.mod noclearall;

%define grid
gamma_MP = 1.95:0.6:2.55;
gamma_FP = -0.05:0.2:0.15;

results=cell(length(gamma_MP),length(gamma_MP));
for i = 1:length(gamma_MP)
    for j = 1:length(gamma_FP)
        set_param_value('gamma_R_PI',gamma_MP(i));
        set_param_value('gamma_tau',gamma_FP(j));
        info=stoch_simul(var_list_);
        if info==0
            results{i,j}=oo_;
        else
            %model could not be solved
            fprintf('model cannot be solved for gamma_R_PI=%3.2f and gamma_tau=%3.2f\n',gamma_MP(i),gamma_FP(j))
        end
    end
end
```

while the mod-file is fully functional, i.e. contains all parameters including the ones you reset later.

P.S.: Never use a Matlab-file with the same name as the Dynare file!  
[Loop\_main.m](https://forum.dynare.org/uploads/default/original/2X/2/2ad7bb7e855c7c4b0d9e830051e5b8f75984534c.m) (690 Bytes)  
[Loop\_example.mod](https://forum.dynare.org/uploads/default/original/2X/c/c499caf584746e1f51b1ec30bd52a133d33ca393.mod) (14.3 KB)

---

<div class="post-metadata">

**Author:** ![Econbee](https://forum.dynare.org/letter_avatar_proxy/v4/letter/e/c57346/32.png) [@Econbee](https://forum.dynare.org/u/Econbee)\
**Post date:** [5 April 2016 13:09 UTC](https://forum.dynare.org/t/question-dynare-in-a-loop-over-a-parameter-space/5398/3 "2016-04-05T13:09:36Z")

</div>

Dear Johannes,

That works. Thank you so much!

By the way, as I run the attached file to search for the posterior mode, I got the following error message if I use Dynare 4.4.3

Cell contents reference from a non-cell array object.  
Error in dynare\_estimation\_init (line 64)  
id = strmatch(options\_.varobs{i}, M\_.endo\_names, ‘exact’);  
Error in dynare\_estimation\_1 (line 81)  
[dataset\_,dataset\_info, xparam1, hh, M\_, options\_, oo\_, estim\_params\_,

bayestopt\_] = …  
Error in dynare\_estimation (line 89)  
dynare\_estimation\_1(var\_list,dname);  
Error in test2 (line 520)  
dynare\_estimation(var\_list\_);  
Error in dynare (line 180)  
evalin(‘base’,fname) ;

If I change to the latest version of the unstable version 2016-04-04, I got the following error message

Error using check\_matlab\_path (line 99)  
You put all the dynare/matlab subfolders in matlab’s path! Only the

dynare/matlab folder (without subfolders)  
should be in the path, Dynare will automatically add any required subfolders in

the path.  
Error in dynare (line 60)  
check\_matlab\_path(change\_path\_flag);

I get the code run successfully when I use a very old version of Dynare, which I downloaded a few years ago, version 2014-12-25.

Could you please help me check what the problem is?

Thank you!  
[mydata.xls](https://forum.dynare.org/uploads/default/original/2X/5/53f2a836b7d66c04c1a3c8c60c029fc5088f8ada.xls) (65.5 KB)  
[test2.mod](https://forum.dynare.org/uploads/default/original/2X/1/1ecb942443ae45af344e2d2983d6f6d31e324f94.mod) (15 KB)

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [5 April 2016 19:46 UTC](https://forum.dynare.org/t/question-dynare-in-a-loop-over-a-parameter-space/5398/4 "2016-04-05T19:46:15Z")

</div>

I cannot replicate any issue.  
To use the unstable version, you should clean your Matlab path from all Dynare-related folders and then add the Dynare/matlab folder of the unstable version to the path (not including subfolders). Note that switching Dynare versions within the same Matlab session can be problematic, because there may be subfolders added from before. Restarting Matlab or manually cleaning the path then helps.

---

<div class="post-metadata">

**Author:** ![Econbee](https://forum.dynare.org/letter_avatar_proxy/v4/letter/e/c57346/32.png) [@Econbee](https://forum.dynare.org/u/Econbee)\
**Post date:** [6 April 2016 02:41 UTC](https://forum.dynare.org/t/question-dynare-in-a-loop-over-a-parameter-space/5398/5 "2016-04-06T02:41:22Z")

</div>

Thanks, Johannes! I followed your suggestion and got all different versions of Dynare running OK. My stable version 4.4.3 on the computer was old (downloaded in 2014). I guess there were bugs in it . I downloaded it again and it worked well now. Thank you again!
