Loop with macro language

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

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

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,



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 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…).




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.