lettura simple

Saving and importing data in Octave

In this Octave lesson, I will explain how to save and load data using files in Octave.

Why is it important to save data? Saving data means storing the data that is in the computer's RAM into a file on the hard disk or other storage device (e.g. USB drive). The data in the computer's RAM is volatile, which means you will lose it when you turn off the computer or close Octave. In contrast, data saved in a file is permanent and can be reloaded into the RAM when you turn on the PC, without having to rewrite it every time.

How to save data

Let me give you a practical example.

Create three arrays named M1, M2, M3:

>> M1=[1 2 3; 4 5 6];
>> M2=[7 8 ; 9 1];
>> M3 = [ 3 4; 5 6; 8 9];

There are now three arrays in memory.

To save all the data in memory, use the command save followed by the file name:

>> save filename.mat

Now clear all the data in memory with the clear command:

>> clear

All the data has been deleted from memory.

If you try to call the matrix M1, Octave returns an error:

>> M1
error: 'M1' undefined near line 1 column 1

To load the data from the file back into memory, use the load command followed by the file name:

>> load filename.mat

Now the data is back in memory.

If you try to call the array M1, you can see its content:

>> M1
M1 =
1 2 3
4 5 6

Note. In this example, I showed you how to save arrays, but the saving function works with all objects and data structures (variables, plots, arrays, etc.). The procedures for saving and loading are always the same."

How to save only specific objects

Octave also allows you to make selective saving. In this case, you need to specify which objects you want to save in the file.

For example, to save only the matrix M1, type:

>> save filename2.mat M1

To save matrices M1 and M2, simply type:

>> save filename3.mat M1 M2

Alternatively, you can also use this syntax to save the data.

>> save("filename3.mat", "M1", "M2")

Note. If you want to add additional objects to the data save, indicate them by inserting an additional parameter after the file name. For example, to save three objects, write save filename3.mat M1 M2 M3 or save("filename3.mat", "M1", "M2", "M3").

To save all objects that begin with the letter M, use the special character *

>> save filename3.mat M*

In any case, loading the data into memory always happens in the same way.

To load the data into memory, you must use the load command.

load filename2.mat

How to load a single object

If you have stored numerous objects in a file, you can opt for selective loading to load only the required objects and avoid loading unnecessary ones.

To do so, you need to specify the specific objects you wish to load into memory.

Why use selective loading? Selective loading is extremely useful when working with big data. If a file contains many objects, loading them into memory can result in unnecessary waste of memory space, slow processing times, and even cause a memory overflow error. In general, it is good practice to load into memory only the data that is necessary for processing.

For example, in the file named filename.mat, you have already saved matrices M1, M2, and M3.

To load only matrix M1 into memory, type:

>> load filename.mat M1

This way, you load matrix M1 into memory, but not matrices M2 and M3 that are also present in the file.

To load matrices M1 and M2 into memory, type:

>> load filename.mat M1 M2

This command loads matrices M1 and M2 into memory but not matrix M3.

Alternatively, you can also use this selective loading syntax:

>> load("filename.mat","M1","M2")

Note. To load three or more objects into memory, add the other objects as additional parameters of the load command. For example, load filename.mat M1 M2 M3

To load all objects that begin with M, type:

>> load filename.mat M*

How to save data file in binary format

Octave also allows you to save data in binary format.

In this case, you need to add the -binary option to the save command.

>> save -binary filename4.mat

To load the data into memory, you can use the same method as before.

>> load filename4.mat

Additional Saving Options

The save command has other very useful options:

  • -append
    appends data to an existing file without deleting any pre-existing data
  • -zip
    compresses the data (useful for big data)
  • -ascii
    saves the data in text format
  • -binary
    saves the data in binary format
  • -hdf5
    saves the data in HDF5 format

If you need to include multiple options in the 'save' command, use this general syntax:

>> save ("-option1", ..., "file", "v1", ...)

To see all the options available for the 'save' command, use Octave's help:

>> help save

Now you have all the necessary information to save and load data in Octave.




Report a mistake or post a question




FacebookTwitterLinkedinLinkedin