Wednesday, November 2, 2011

Two GNU Octave Tips

1. Changing directories 

The standard command looks very much like the *nix "cd".

cd NameOfDirectory;

But if "NameOfDirectory" was stored in a variable (DirName  = "NameOfDirectory";), then trying something like,

cd DirName;

would fail because Octave would try to literally look for a directory named DirName. In *nix command lines one would circumvent this issue by "cd $DirName", but this does not work in Octave.

The solution is to use parenthesis. So the following does the trick:

cd (DirName);

2. Quotes: One can use the system command to issue instructions to the shell from within a Octave program. So to list the files in the present working directory one would say:

system('ls');

However, if one wanted to use a Unix program or utility that itself uses quotes, then there are problems in parsing. So something like,

system('awk '{print $0}' infile > outfile')

would not work. The solution is again quite simple. Use double quotes which cause the argument within the double quotes to be interpreted. Hence,

system("awk '{print $0}' infile > outfile")

does what one would expect it to do.

No comments: