Saving and loading objects


Saving and loading objects in LTPDA is straightforward. You have two formats available to you: the standard MATLAB .mat binary format, or the text-based XML format.

Saving single objects

Suppose you have an AO in your MATLAB workspace, you can save it to disk like this:

      % Generate a time-series of random numbers      
      a = ao.randn(10, 10);
      % Save to a file called 'a.mat'      
      save(a);
This will save the file to disk in MAT format with the filename 'a.mat'. If you want to specify a filename, you can do that by doing:
      % Generate a time-series of random numbers      
      a = ao.randn(10, 10);
      % Save the AO to a MAT file      
      save(a, 'myNiceAO.mat');
You can also use a plist to configure save:
      f = miir(plist('type', 'lowpass', 'fc', 0.1, 'fs', 10, 'order', 1))
      save(f, plist('filename', 'myNiceFilter.mat'));

Saving multiple objects

You can save multiple objects in to one file with LTPDA:

      a1 = ao.randn(10, 10);
      a2 = ao.randn(10, 100);
      save(a1, a2, 'myObjects.mat');
      % If you have a vector of objects...
      a = [a1 a2];
      save(a, 'myVectorOfObjects.mat');
To save a vector of objects to individual files, you can do:
      a1 = ao(plist('waveform', 'sine wave', 'a', 1, 'f', 1, 'phi', pi/2, 'fs', 50, 'nsecs', 10, 'name', 'sig1'))
      a2 = ao(plist('waveform', 'sine wave', 'a', 1, 'f', 0.2, 'phi', pi/4, 'fs', 50, 'nsecs', 10, 'name', 'sig2'))
      a  = [a1 a2];
      save(a, plist('individual files', true));
If you want to specify a filename, then as before, you do:
      a1 = ao(plist('waveform', 'sine wave', 'a', 1, 'f', 1, 'phi', pi/2, 'fs', 50, 'nsecs', 10, 'name', 'sig1'))
      a2 = ao(plist('waveform', 'sine wave', 'a', 1, 'f', 0.2, 'phi', pi/4, 'fs', 50, 'nsecs', 10, 'name', 'sig2'))
      a  = [a1 a2];
      save(a, plist('filename', 'myAOs.xml', 'individual files', true));

Loading objects from disk

Loading objects from disk is typically done using the relevant constructor. However, this does imply that you know the class of the object in the file on disk. If you don't, you need to try to load with a particular constructor (say, AO) then if that is not correct, the error message will tell you the class of the object in the file.

Let's load one of the AOs we saved above:

      a = ao('myNiceAO.mat');
Above we saved a file called 'myNiceFilter.mat'. If we don't know that this is an miir object, and instead try to load it as an AO, then you will see the following:
      >> a = ao('myNiceFilter.mat');
      Error using ltpda_uo/fromFile (line 174)
You tried to load objects of class [ao] from myNiceFilter.mat, but that file contains objects of class [miir]
Error in ao (line 348)
			obj = fromFile(obj, args{1});



©LTP Team