loop with macro language

This forum is closed. You can read the posts but cannot write. We have migrated the forum to a new location where you will have to reset your password.
Forum rules
This forum is closed. You can read the posts but cannot write. We have migrated the forum to a new location (https://forum.dynare.org) where you will have to reset your password.

loop with macro language

Postby pburriel » Thu Jun 18, 2009 8:41 am

Hi to all,

I am using the new macrolanguage to write loops within a .mod file and is working fine in general.

However, the problem is that one cannot use a variable previously defined to set anything in the macrolanguage. For example, one may want to set the loop range from the size a vector previously defined (or loaded from another exercise), but this is not possible.

That is, if for example you want to define a number of variables using a loop, you may write

@#define n_type = [1:2]
@#for type in n_type
g_@{type}
@#endfor

and it works fine, creating two variables with the names g_1 and g_2

However, if you write instead

load xvector.mat;
@#define n_type = [1:size(xvector,1)]
@#for type in n_type
g_@{type}
@#endfor

It will give you an error message:
ERROR in macro-processor: file.mod: Unknown variable: size

That is, the macrolanguage does not accept any input from outside of itself.
Why is this? Can this be solved?

Thanks for your help,

Pablo
pburriel
 
Posts: 23
Joined: Wed Jun 29, 2005 4:42 am

Re: loop with macro language

Postby SébastienVillemot » Sun Jun 21, 2009 3:01 pm

Hi,

To put it short, the macro-language indeed doesn't interact with MATLAB statements such as "load". You cannot use a MATLAB variable inside a macro-language statement. The only variables that you can use inside the macro-language are those defined by a macro language statement (@#define or @#for).

This is so by very construction of the macro-language: its commands are processed at the very beginning of the Dynare toolchain, far before any MATLAB computation is run. In its essence, the macro-language is a set of functions which manipulate the text of the MOD file: it will replicate or substitute parts of the text of the MOD file, but is completely ignorant of the meaning of the MOD file commands. To put it otherwise, the Dynare macro-language could operate on any text file; it is completely orthogonal to the Dynare commands (stoch_simul, estimation...) and to MATLAB commands (load, ...).

If you want to use MATLAB variables inside loops, you need to use MATLAB loops, not macro-language loops (see http://www.dynare.org/DynareWiki/HowtoLoops for an example). However this may not be applicable to your problem, because with MATLAB loops you cannot loop over Dynare declarations (such as var, varexo...).

Best

Sébastien
Sébastien Villemot
Economist at OFCE – Sciences Po
SébastienVillemot
 
Posts: 706
Joined: Fri Dec 07, 2007 2:29 pm
Location: Paris, France

Re: loop with macro language

Postby pburriel » Mon Jun 22, 2009 10:46 am

Sebastien,

That is what I thought.

I must say the macrolanguage is a very powerful tool for dynare, specially when working with multisector models, however it would be even more if it could interact with matlab commands.

Thanks,

Pablo
pburriel
 
Posts: 23
Joined: Wed Jun 29, 2005 4:42 am


Return to Dynare help

Who is online

Users browsing this forum: Google [Bot] and 11 guests