4.2 Programming with physica macros

When you quit, physica also forgot all of its settings, not just the data values, so you had to re-enter the label strings and all other commands. In general, computers are very good at repetitive tasks, and one should never have to type the same thing twice! In physica you can create ``programs'' of physica commands called macros.

For example, quit or move to another window, and use your favourite editor to create a plain text file named graph2d.pcm, with the following lines in it:

read file.dat x,y,dy
scalar\vary a
fit y=a*x
fit\upd f
lab\x `Time, s'
lab\y `Distance, m'
clear
set pchar -10
gra x,y,dy
set pchar 0
gra\noax x,f

These are exactly the same commands that we have used before, except we have abbreviated a few of them (non-ambiguous abbreviations are allowed in physica). When you re-start physica now, all you need to do to repeat the plot of Figure 1 is:

PHYSICA: execute graph2d

Since we have used the default file extension in naming the macro (.pcm), we do not need to type it in, physica supplies it automatically. The above line ``executes'' a macro contained in the file graph2d.pcm with all the right commands.

To analyze another set of data from a similar experiment you would have to edit the macro file graph2d.pcm, replace the file name file.dat with a new one, and then repeat the above.

Actually, there is a way to build a ``universal'' macro! Change the first line in your macro to

read ?1 x,y,dy

By specifying ?1 in place of the file name you are telling physica to get the file name from you at the time of execution of the macro, like this:

PHYSICA: execute graph2d file.dat

meaning ``read the data from the file whose name is contained in the parameter No.1 (?1) on the command line into vectors x, y, and dy''. And the name we supply on the above line is the same file.dat as before. For another set of data you could generate the graph with a single physica command, by simply specifying a different filename instead of file.dat!

Test your understanding of the above macro by modifying it to allow passing the plotting character code as the second parameter. Within the macro you would then refer to it as ?2 and from physica you would call it up like this:

PHYSICA: exe graph2d file.dat 10

to use triangles again (note the abbreviation of the word execute); or

PHYSICA: @graph2d file.dat 4

to use crosses (for example) at the data points. The symbol @ is another shorthand for execute.

Up: 4. A simple example Next: 4.3 Getting help Previous: 4.1 Reading data into physica