Page 1 of 1

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

PostPosted: Thu Feb 28, 2013 8:59 am
by Philipp
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

load xy_results.mat
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

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

PostPosted: Thu Feb 28, 2013 3:15 pm
by kyri82
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.
eval[(load xy_results', num2str(i), ']) ;

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

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

PostPosted: Thu Feb 28, 2013 6:22 pm
by Philipp
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

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

PostPosted: Thu Feb 28, 2013 11:00 pm
by jpfeifer
Define a cell containing the blocks over which you want to iterate, e.g.
Code: Select all
ending_cell={'_a',...
'_b'}

Then loop over the endings
Code: Select all
for ii=1:length(ending_cell)
end

Inside of the loop, use the eval command, where you access the ending
Code: Select all
eval(['plot(HOR,oo_.irfs.y_', ending_cell{ii,1}, ','y',HOR,oo_.irfs.c_', ending_cell{ii,1},'b')']) ;

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

PostPosted: Fri Mar 01, 2013 9:24 am
by kyri82
very clever!! ;)

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

PostPosted: Fri Mar 01, 2013 3:07 pm
by Philipp
indeed, thank you very much for your helpful comments!

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

PostPosted: Wed Mar 13, 2013 8:34 pm
by Philipp
Hi guys,

I just wanted to update on the figure discussion, I got a pretty nice figure using

Code: Select all
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


thanks again for your ideas!
Philipp