# DSGE multiple shocks, subfig for each shock/variable?!

Hi all,

I want to do the following:

I have a file “xy_results.mat”, which has inside of it among others an object called “oo_”, and therein among others an object called “irfs”. In “irfs” I have several vectors {y_a, c_a, i_a, y_b,c_b, i_b,…}. Now I would like to have a loop which creates subplots for all {y_a,y_b,…}, {c_a,c_b,…} etc and also subplots in another figure which contains all elements of {_a}, {_b}, etc.

Who can help me on this? Doing it manually I tried something like

HOR=1:1:40;
plot(HOR,oo_.irfs.y_a,‘y’,HOR,oo_.irfs.c_a,‘b’)
which works fine, but as I have many entries in irfs and many differens “.mat” I’d prefer to have a loop that does the copy paste for me.

Could somebody please help me out on this issue, most likely with a sample code, I would appreciate your help very much! Best, Philipp

What I could suggest to you is to have “extentions” as numbers, instead of letters. That is, instead of having y_a and y_b, you could have y_1 and y_2. Then, you could use the eval command to do something like:

for i=1:3
eval(‘plot(HOR,oo_.irfs.y_’, num2str(i), ‘,‘y’,HOR,oo_.irfs.c_’, num2str(i),‘b’)’]) ;
end

That would execute:

plot(HOR,oo_.irfs.y_1,‘y’,HOR,oo_.irfs.c_1,‘b’)
plot(HOR,oo_.irfs.y_2,‘y’,HOR,oo_.irfs.c_2,‘b’)
plot(HOR,oo_.irfs.y_3,‘y’,HOR,oo_.irfs.c_3,‘b’)
… and so on

You can also load in a similar way .mat files if they have the same name but different numbers at the end.

Just a warning: the command above (the one with plot) might not work because eval becomes a bit complicated if there are " ’ " within the command itself. In any case, check “eval” and “num2str” in help.

Hope that helps.

Kyriacos

Hi kyri82,

I have tried with eval(), but my problem is that I have troubles editing the strings in my objects. The extensions “_a” and “_b” come from shocks in dynare, usually something like “_ea” (technology) or else. Now how do I change “_ea” to “1” and “_eb” to “2” and so forth?

Best

Philipp

Define a cell containing the blocks over which you want to iterate, e.g.

```ending_cell={'_a',... '_b'}```
Then loop over the endings

```for ii=1:length(ending_cell) end```
Inside of the loop, use the eval command, where you access the ending

```eval('plot(HOR,oo_.irfs.y_', ending_cell{ii,1}, ','y',HOR,oo_.irfs.c_', ending_cell{ii,1},'b')']) ; ```

very clever!!

``` HOR=1:1:40; var ={'y','pi','w','k','i','c','r','q','e'}; ending_cell={'_na','_nb','_ni','_nl','_ng','_npi','_er','_ew','_ep','_eq'}; figure for jj=1:length(var) subplot(3,3,jj) for ii=1:length(ending_cell) [eval(['oo_.irfs.' var{1,jj}, ending_cell{1,ii}])]; hold on plot(HOR,[eval(['oo_.irfs.' var{1,jj}, ending_cell{1,ii}])],'LineWidth',1) title('Response of ' var{1,jj}] ) end end```