# Q: combining IRFs into subplotted figures & iterating?

**URL:** <https://forum.dynare.org/t/q-combining-irfs-into-subplotted-figures-iterating/3609>\
**Category:** Stochastic simulations\
**Created:** [8 January 2014 13:21 UTC](https://forum.dynare.org/t/q-combining-irfs-into-subplotted-figures-iterating/3609 "2014-01-08T13:21:14Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![GOODLUCK](https://forum.dynare.org/letter_avatar_proxy/v4/letter/g/65b543/32.png) [@GOODLUCK](https://forum.dynare.org/u/GOODLUCK)\
**Post date:** [8 January 2014 13:21 UTC](https://forum.dynare.org/t/q-combining-irfs-into-subplotted-figures-iterating/3609/1 "2014-01-08T13:21:14Z")

</div>

Hi all,

I am really frustrated by the following issue but didn’t find answers in the forum, so I’m afraid I had to raise the frequently asked problem again. The setup is that I have two models, and want to compare the IRFs of each endogenous variables due to exo shocks. Say, model 1 and 2, both have endo variables y and c, exo shocks ea and em. So, the names of two models’ variables are totally the same. Now, I want to plot two IRF figures, one for response of shock ea, another em.Each figure has two subfigures,one for response of y,another c.And each subfigure has two curves, one stands for IRF of model 1, another model 2. By learning from related posts in this forum, I just write the following code, but failed to run it out.

```auto
dynare model1.mod
load('model1_results.mat', 'oo_')
irf1=oo_.irfs;
save irf1
load irf1

dynare model2.mod
load('model2_results.mat', 'oo_')
irf2=oo_.irfs;
save irf2
load irf2

ending_cell={'_ea','_em'};

for ii=1:length(ending_cell)
HOR=1:1:40;
var={'y','c'};
figure
for jj=1:length(var)
subplot(1,2,jj) 
eval('irf1.' var{1,jj},ending_cell{1,ii}]);
eval('irf2.' var{1,jj},ending_cell{1,ii}]);
hold on
plot(HOR,[eval(['irf1.' var{1,jj},ending_cell{1,ii}])],'LineWidth',1,'-k',HOR,[eval(['irf2.' var{1,jj},ending_cell{1,ii}])],'LineWidth',1,'--r')
title([var{1,jj}] )
end
end
```

Matlab just stopped by warning"Undefined variable “irf1” or class “irf1.y\_ea”."I guess the contents of irf1 are just overlapped by irf2, because all the variable names are the same among the two models. But I don’t know how to get rid of the problem.Could anyone kindly give me another hand please?

Many thanks indeed.

GOODLUCK

---

<div class="post-metadata">

**Author:** ![GOODLUCK](https://forum.dynare.org/letter_avatar_proxy/v4/letter/g/65b543/32.png) [@GOODLUCK](https://forum.dynare.org/u/GOODLUCK)\
**Post date:** [8 January 2014 16:11 UTC](https://forum.dynare.org/t/q-combining-irfs-into-subplotted-figures-iterating/3609/2 "2014-01-08T16:11:56Z")

</div>

I’ve tried dealing with this problem by reinstalling DYNARE4.40 exclusively in my personal computer under path C:, but with things unchanged. The error just displays as :

Undefined variable “irf1.” or class “irf1.y\_epsilon\_A”.

Error in plotting (line 37)  
eval(‘irf1.’ var{1,jj},ending\_cell{1,ii}]);

I am wondering whether things could be improved or fixed. I have no idea how to figure out the wrongs since I am also quite new to Matlab. So sorry that I have so many questions, yet I have to ask anyone has any idea?

Best,

GOODLUCK

---

<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:** [10 January 2014 17:35 UTC](https://forum.dynare.org/t/q-combining-irfs-into-subplotted-figures-iterating/3609/3 "2014-01-10T17:35:43Z")

</div>

You either need to load irf1 after the last call to Dynare or use the noclearall option.

---

<div class="post-metadata">

**Author:** ![GOODLUCK](https://forum.dynare.org/letter_avatar_proxy/v4/letter/g/65b543/32.png) [@GOODLUCK](https://forum.dynare.org/u/GOODLUCK)\
**Post date:** [12 January 2014 13:03 UTC](https://forum.dynare.org/t/q-combining-irfs-into-subplotted-figures-iterating/3609/4 "2014-01-12T13:03:26Z")

</div>

Thank you very much, jpfeifer. I’ve done it.

---

<div class="post-metadata">

**Author:** ![yufeng](https://forum.dynare.org/letter_avatar_proxy/v4/letter/y/ccd318/32.png) [@yufeng](https://forum.dynare.org/u/yufeng)\
**Post date:** [14 April 2014 11:57 UTC](https://forum.dynare.org/t/q-combining-irfs-into-subplotted-figures-iterating/3609/5 "2014-04-14T11:57:06Z")

</div>

> [@GOODLUCK](#):
>
> Thank you very much, jpfeifer. I’ve done it.

Hi,GOODLUCK,

I meet the same question as you, but I still don’t know how to plot them? Can you tell me how you do it? Thank you!

yufeng

---

<div class="post-metadata">

**Author:** ![lah89](https://forum.dynare.org/letter_avatar_proxy/v4/letter/l/8491ac/32.png) [@lah89](https://forum.dynare.org/u/lah89)\
**Post date:** [25 May 2014 17:31 UTC](https://forum.dynare.org/t/q-combining-irfs-into-subplotted-figures-iterating/3609/6 "2014-05-25T17:31:25Z")

</div>

I d like to see an example solution as well 🙂  
In particular I want to combine 2 irf-outputs from stoch\_simul showing the IRFs for certain endogenous variables, for the same model (same endogenous variables, same shock, but different shocksizes) in one picture.

---

<div class="post-metadata">

**Author:** ![Mylene](https://forum.dynare.org/letter_avatar_proxy/v4/letter/m/d26b3c/32.png) [@Mylene](https://forum.dynare.org/u/Mylene)\
**Post date:** [2 April 2015 03:02 UTC](https://forum.dynare.org/t/q-combining-irfs-into-subplotted-figures-iterating/3609/7 "2015-04-02T03:02:15Z")

</div>

Hello!

I am very new to Matlab. I’ve spent so many days searching for the correct codes in combining plots. I tried also this code, but yes, it is with error. How to remove the error? Jpfeifer suggested to use a noclearall option or load irf1. How exactly to do the suggestion?

Thanks so much for the help in advance.

---

<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 2015 09:34 UTC](https://forum.dynare.org/t/q-combining-irfs-into-subplotted-figures-iterating/3609/8 "2015-04-02T09:34:10Z")

</div>

Please provide more information. Provide both the files and precise instructions on what you are trying to plot from the two mod-files.

---

<div class="post-metadata">

**Author:** ![Zeinab](https://forum.dynare.org/letter_avatar_proxy/v4/letter/z/e95f7d/32.png) [@Zeinab](https://forum.dynare.org/u/Zeinab)\
**Post date:** [2 June 2015 18:04 UTC](https://forum.dynare.org/t/q-combining-irfs-into-subplotted-figures-iterating/3609/9 "2015-06-02T18:04:28Z")

</div>

```auto
dynare Flexible_1.mod
load('Flexible_1_results.mat', 'oo_')
irf1=oo_.irfs;
save irf_1
load irf_1

dynare Flexible_2.mod
load('Flexible_2_results.mat', 'oo_')
irf2=oo_.irfs;
save irf_2
load irf_2

ending_cell={'_eps_A'};

for ii=1:length(ending_cell)
HOR=1:1:20;
var={'Welf'};
figure
for jj=1:length(var)
subplot(1,2,jj) 
eval('IRF_1.' var{1,jj},ending_cell{1,ii}]);
eval('IRF_2.' var{1,jj},ending_cell{1,ii}]);
load IRF_2
end
end
```

---

<div class="post-metadata">

**Author:** ![Zeinab](https://forum.dynare.org/letter_avatar_proxy/v4/letter/z/e95f7d/32.png) [@Zeinab](https://forum.dynare.org/u/Zeinab)\
**Post date:** [2 June 2015 18:04 UTC](https://forum.dynare.org/t/q-combining-irfs-into-subplotted-figures-iterating/3609/10 "2015-06-02T18:04:57Z")

</div>

he doen’t work with me at all ! could you help me please

---

<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:** [3 June 2015 11:21 UTC](https://forum.dynare.org/t/q-combining-irfs-into-subplotted-figures-iterating/3609/11 "2015-06-03T11:21:18Z")

</div>

You must either load the first mat-file after the second call to Dynare or invoke the second call to Dynare with the

```auto

```

option. Otherwise, everything you do before

```auto

```

will be deleted.

---

<div class="post-metadata">

**Author:** ![medad](https://forum.dynare.org/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@medad](https://forum.dynare.org/u/medad)\
**Post date:** [24 July 2015 05:37 UTC](https://forum.dynare.org/t/q-combining-irfs-into-subplotted-figures-iterating/3609/12 "2015-07-24T05:37:32Z")

</div>

Hi all

Please hint me in this code:

```auto
    dynare dr.mod
    load('dr_results.mat', 'oo_')
    irf1=oo_.irfs;
    save irf_1
    load irf_1

    dynare rd.mod noclearall
    load('rd_results.mat', 'oo_')
    irf2=oo_.irfs;
    save irf_2
    load irf_2
    
    ending_cell={'_epsr'};

    for ii=1:length(ending_cell)
HOR=1:1:40;
var={'y','paii','gam'};
figure

for jj=1:length(var)
subplot(1,2,jj)
eval('irf1.' var{1,jj},ending_cell{1,ii}]);
eval('irf2.' var{1,jj},ending_cell{1,ii}]);
hold on
plot(HOR,[eval(['irf1.' var{1,jj},ending_cell{1,ii}])],'LineWidth',1,'-k',HOR,[eval(['irf2.' var{1,jj},ending_cell{1,ii}])],'LineWidth',1,'--r')
title([var{1,jj}] )
end
end
```

with this Error:

```auto
ERROR: Untitled.mod: line 18, col 4: syntax error, unexpected EQUAL
```

Thank you!  
Best wishes…

---

<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:** [24 July 2015 08:02 UTC](https://forum.dynare.org/t/q-combining-irfs-into-subplotted-figures-iterating/3609/13 "2015-07-24T08:02:55Z")

</div>

This is a problem with the mod-file Untitled.mod. The code you pasted is Matlab code and not Dynare code. Run this directly from Matlab.

---

<div class="post-metadata">

**Author:** ![medad](https://forum.dynare.org/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@medad](https://forum.dynare.org/u/medad)\
**Post date:** [24 July 2015 09:09 UTC](https://forum.dynare.org/t/q-combining-irfs-into-subplotted-figures-iterating/3609/14 "2015-07-24T09:09:19Z")

</div>

Thank you very much, jpfeifer.  
But after ruining all irf1 and irf2 separately, this error occurred again:

```auto

```

Thank you for your kindly reply.

---

<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:** [24 July 2015 12:08 UTC](https://forum.dynare.org/t/q-combining-irfs-into-subplotted-figures-iterating/3609/15 "2015-07-24T12:08:06Z")

</div>

Be more careful. If you define

`irf1
`  
you cannot save

```auto
irf_1
```

Also, you need to load the files after the last call to Dynare, not after you saved them.

---

<div class="post-metadata">

**Author:** ![Dsge3](https://forum.dynare.org/letter_avatar_proxy/v4/letter/d/e95f7d/32.png) [@Dsge3](https://forum.dynare.org/u/Dsge3)\
**Post date:** [3 July 2018 15:46 UTC](https://forum.dynare.org/t/q-combining-irfs-into-subplotted-figures-iterating/3609/16 "2018-07-03T15:46:24Z")

</div>

Dear all,  
For the same reasons, I have used the following code to combine two IRF’s:

```auto
 dynare MJT.mod
load('MJT_results.mat', 'oo_')
irf1=oo_.irfs;
save irf1
load irf1

dynare TMJ.mod noclearall
load('TMJ_results.mat', 'oo_')
irf2=oo_.irfs;
save irf2
load irf2

ending_cell={'_E_POS'};

for ii=1:length(ending_cell)
    HOR=1:1:25;
    var={'GPI', 'C'};
    figure
    
    for jj=1:length(var)
        subplot(1,2,jj)
        eval(['irf1.' var{1,jj},ending_cell{1,ii}]);
        eval(['irf2.' var{1,jj},ending_cell{1,ii}]);
        hold on
        plot(HOR,[eval(['irf1.' var{1,jj},ending_cell{1,ii}])],'LineWidth',2.5,'-k',HOR,[eval(['irf2.' var{1,jj},ending_cell{1,ii}])],'LineWidth',2.5,'--r') ;
        title([var{1,jj}] )
    end
end

```

the error message is:

```auto
Error using plot
Data must be a single matrix Y or a list of pairs X,Y.

```

What can I do to get the right results please?

---

<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:** [4 July 2018 12:47 UTC](https://forum.dynare.org/t/q-combining-irfs-into-subplotted-figures-iterating/3609/17 "2018-07-04T12:47:37Z")

</div>

You can only have one `LineWidth`- command. It should be

```auto
plot(HOR,[eval(['irf1.' var{1,jj},ending_cell{1,ii}])],'-k',HOR,[eval(['irf2.' var{1,jj},ending_cell{1,ii}])],'LineWidth',2.5,'--r') ;

```

---

<div class="post-metadata">

**Author:** ![simsima](https://forum.dynare.org/letter_avatar_proxy/v4/letter/s/7cd45c/32.png) [@simsima](https://forum.dynare.org/u/simsima)\
**Post date:** [7 January 2019 13:57 UTC](https://forum.dynare.org/t/q-combining-irfs-into-subplotted-figures-iterating/3609/18 "2019-01-07T13:57:17Z")

</div>

```auto
dynare ssfriction_fixe.mod
load('ssfriction_fixe_results.mat', 'oo_')
irf1=oo_.PosteriorIRF.dsge.Var;
save irf1
load irf1

dynare ssfriction_amenage.mod
load('ssfriction_amenage_results.mat', 'oo_')
irf2=oo_.PosteriorIRF.dsge.Var;
save irf2
load irf2

ending_cell={'_e_z'};

for ii=1:length(ending_cell)
HOR=1:1:25;
var={'lab','inv'};
figure
for jj=1:length(var)
subplot(1,2,jj) 
eval(['irf1.' var{1,jj},ending_cell{1,ii}]);
eval(['irf2.' var{1,jj},ending_cell{1,ii}]);
load irf1
load irf2
hold on
plot(HOR,[eval(['irf1.' var{1,jj},ending_cell{1,ii}])],'-k',HOR,[eval(['irf2.' var{1,jj},ending_cell{1,ii}])],'LineWidth',25,'--r')
title([var{1,jj}] )
end
end

```

when i run this code i get this error msg:

"Error using plot Vectors must be the same length.

Error in pf (line 26)  
plot(HOR,[eval([‘irf1.’ var{1,jj},ending\_cell{1,ii}])],’-k’,HOR,[eval([‘irf2.’  
var{1,jj},ending\_cell{1,ii}])],‘LineWidth’,25,’–r’)"

can you help me plz !

---

<div class="post-metadata">

**Author:** ![punnoosejacob](https://forum.dynare.org/user_avatar/forum.dynare.org/punnoosejacob/32/5976_2.png) [@punnoosejacob](https://forum.dynare.org/u/punnoosejacob)\
**Post date:** [7 January 2019 23:38 UTC](https://forum.dynare.org/t/q-combining-irfs-into-subplotted-figures-iterating/3609/19 "2019-01-07T23:38:25Z")

</div>

Did you try load irf1 and irf2 right before the loop?

```auto
for jj=1:length(var)
```

and did you check the lengths of the plotted vectors as the error message claims…?  
Cheers  
Reuben

---

<div class="post-metadata">

**Author:** ![simsima](https://forum.dynare.org/letter_avatar_proxy/v4/letter/s/7cd45c/32.png) [@simsima](https://forum.dynare.org/u/simsima)\
**Post date:** [8 January 2019 11:01 UTC](https://forum.dynare.org/t/q-combining-irfs-into-subplotted-figures-iterating/3609/20 "2019-01-08T11:01:03Z")

</div>

yes, when i changed the lengths of the plotted vectors it solved the problem. But now i get a different error msg : " Data must be a single matrix Y or a list of pairs X,Y" even if i’m using a one LineWidth- command.

[Next page](https://forum.dynare.org/t/q-combining-irfs-into-subplotted-figures-iterating/3609.md?page=2)
