Saving and loading GAUSS matrix files

Saving and loading GAUSS matrix files

GAUSS allows you to read and write data from many different file types such as Excel, ASCII text and database files. All of these file formats have their place, but you should learn to read and write native GAUSS matrix files because it is very simple and very fast.

  1. Basic saving and loading
  2. Specifying a different file, path or variable name
  3. Setting the save path and load path
  4. FAQ/Troubleshooting

Basic saving and loading

Let’s start by creating a small matrix and saving it with the GAUSS save command:

//create a 4x2 matrix
x = { 3.2 5.2,
      5.3 6.3,
      2.5 1.5,
      6.3 9.2 };

//save the data in 'x'
//to a GAUSS matrix file
save x;

In its most basic usage, as seen above, the GAUSS save command saves the contents of a GAUSS matrix to a file:

  1. with the same name as the GAUSS variable, but with a .fmt file extension (in this case x.fmt)
  2. located in the current GAUSS working directory

We can load the file we created with the load command:

load x;

As you may have guessed, the above line of code will look for a file named x.fmt in the current working directory and assign its contents to a GAUSS variable called x.

Specifying a different file, path, or variable name

We can save the x matrix created above, as the file new_var.fmt with the following statement:

save new_var.fmt=x;

To specify a different path for the file, simply add the full path to the file name like this:

save C:\gauss\myproject\new_var.fmt=x;

To set the file name and path with a string variable, prepend the variable with the caret (^) operator:

my_new_file = "C:\\gauss\\myproject\\new_var.fmt";
save ^my_new_file=x;

Notice that the file (and optional path) are on the left side of the equal sign. This is because the file is what is being assigned to. The variable, x, is on the right side of the equal sign because it is the source of the assignment.

When load‘ing a matrix file into a GAUSS variable with a different name, the order from above is reversed. Since the GAUSS variable is being assigned to, it is now on the left side of the equals sign. For example:

load new_x=x.fmt;

or

load new_x=C:\gauss\myproject\x.fmt;

or

my_data_file="C:\\gauss\\myproject\\x.fmt";
load new_x=^my_data_file;

Setting the save path and load path

If you have a separate directory for the data in your project, it would be nice to tell GAUSS the path once rather than specifying it each time you use the save or load command. You can do this by setting the save path and the load path.

You set the save path by passing the word path as a flag to the save command. For example:

save path=C:\gauss\myproject;

After executing the above command, all subsequent variables save‘d with the save command will be placed in the C:\gauss\myproject directory. So this code snippet:

save path=C:\gauss\myproject;
save x;
save y;
save z;

will produce the same effect as:

save C:\gauss\myproject\x.fmt=x;
save C:\gauss\myproject\y.fmt=y;
save C:\gauss\myproject\z.fmt=z; 

The load path works in the same way as the save path:

load path=C:\gauss\myproject\mydata;

This sets the location in which subsequent calls to the load command will look for matrix files.

FAQ/Troubleshooting

Q: When trying to use the load command, I get: error G0014 file not found. What might be the problem?

A: The problem is most likely one of the following:

  1. The file does not exist
  2. The file is in a different directory
  3. The file is of the wrong type

Q: Can I edit or view the contents of an .fmt file without loading the data into GAUSS?

A: No, .fmt files are saved in a binary format that is not human readable.

Q: Is there a way to list all the .fmt files in a directory?

A: Yes, you can list all of the files that match a particular pattern, such as ending with .fmt, with the filesa command:

print filesa("*.fmt");

The asterisk (*) stands for any character or characters. So when you pass "*.fmt" to filesa, it will list all files in your current working directory that end with .fmt.