Masoud Nazari
Masoud Nazari

Reputation: 496

how to save and restore an object into a file in matlab?

I have a class like this:

classdef my_class
properties
    prop_a
    prop_b
end

methods(Static)
    ...
    function o = load()
       ??? 
    end

end

methods
    function save(o)
       ??? 
    end
    ...
end end

Please help me to write save and load methods. name of methods is clear and they should save and restore an object from a text file...

the properties are quite dynamic and have different row/col number.

Upvotes: 0

Views: 55

Answers (1)

krisdestruction
krisdestruction

Reputation: 1960

As documented in the save function, you can save variables to file. As documented in the load function, you can load variables from file.

methods(Static)
    function obj = loadFromFile( filename )
        obj = load( filename );
    end
end

methods
    function saveToFile( obj, filename )
        save( 'test', 'obj' );
    end
end

Caveat

  • Note that you need to specify the full path for the filenames or the object will be saved/loaded to/from the working directory.

Upvotes: 1

Related Questions