Skip to content

make_from_template.m

Calvin Eiber edited this page Jun 30, 2021 · 1 revision

Templating engine.

tools.makeFromTemplate reads a ..template file and evaluates meta-expressions in that file, outputting a file with name ".". This allows Matlab to modify model scripts for various other pieces of software (GMSH, Neuron) while enabling the underlying scripts to be written mostly in the target interpreter's source language with only a few meta-expressions to allow matlab to, e.g. perform parameter sweeps.

tools.makeFromTemplate is part of package +tools

Template syntax

Currently, four kinds of meta-expression are implemented:

  • #ifdef / #endif blocks
  • #postpro (#post, #postprocess) blocks
  • $variable-name replacement
  • @[expression] evaluation

If the .*.template file contains #ifdef X / #endif, then anything which falls between those lines is only included if the string "X" was passed into makeFromTemplate.

If the .*.template file contains $variable-name expressions, the string $X is replaced with the argument "X" (passed in using key-value syntax e.g. make_( template , 'radius', 5) OR with the matlab variable "X" in the local workspace. The expression >> make_( template, 'radius', 5); is equivalent to >> radius = 5; make_( template );

tools.makeFromTemplate will generate a helpful error if a $variable is used in the .*.template file which can't be found. A double $$ is used to indicate a literal $ in the output file; no variable replacement is performed.

You can also call make_(file,'-makev')) which will create a file with the extension .*.variable, which can then be used to define variables and parameter sweeps using the syntax make_(file,'-usev'); or make_(file,'-sweep'); for parameter sweeps.

If the .*.template file contains @[expression], the matlab code "expression" will be evaluated in the local workspace. This can be combined with variable names (e.g. Mesh.CharacteristicLengthMax = @[$radius / 100]; ) or used to evaluate arbitrary matlab code (e.g. Mesh.CharacteristicLengthMax = @[my_lenmax_fun(args)]; )

For both $var and @[expr] replacement, numerical values are converted to text, and text is used as-is. Attempting to insert non-numeric, non- text information will result in a corrupted output file.

By default, the output file has the same name as the input file but a different output file can be specified by _make(file,'-output',output_filename)

Clone this wiki locally