Error: octave_base_value::save_ascii(): wrong type argument 'object'

# Problem description
First problem occurs when saving objects defined with classdef. Second problem is saveobj and loadobj usage.

Exemple:
testclass.m (282 Bytes)

classdef testclass

    properties
		test = "0";
	end
	
	methods
		function b = saveobj(a)
			b.test = "stringSaved";
		endfunction
	endmethods
		
	methods (Static = true)
		function b = loadobj(a)
			b = a;
			b.test = "stringLoaded";
		endfunction
	endmethods
	
endclassdef

The following script replicate the first problem:

oTest = testclass ();
save(“oTest.mat”, “oTest”);

This script result in the following error:

error: octave_base_value::save_ascii(): wrong type argument ‘object’

However the following script works better :

oTest = testclass ();
save("-v7", “oTest.mat”, “oTest”);

There is a warning but the object is saved:

“warning: struct: converting a classdef object into a struct overrides the access restrictions defined for properties”

The second problem is about saveobj and loadobj methods. I expected that Octave would have saved the structure “b” with the field “test” with value “stringSaved”. Also I expected that Octave would have loaded the structure “b” with the field “test” with value “stringSaved”. It looks like if loadobj and saveobj are not executed. The following script shows that test property is still at value 0 after loading.

Script:

1 oTest = testclass ();
2 printf("\noTest.test = “);
3 disp(oTest.test);
4 save(”-v7", “oTest.mat”, “oTest”);
5 clear(“oTest”);
6 load(“oTest.mat”);
7 printf("\noTest.test(loaded) = ");
8 disp(oTest.test);

Output:

oTest.test = 0
warning: struct: converting a classdef object into a struct overrides the access restrictions defined for properties. All properties are returned, including
private and protected ones.
warning: called from
tst3 at line 4 column 1

warning: struct: converting a classdef object into a struct overrides the access restrictions defined for properties. All properties are returned, including
private and protected ones.
warning: called from
tst3 at line 4 column 1

warning: load: classdef element has been converted to a struct
warning: called from
tst3 at line 7 column 1

oTest.test(loaded) = 0

Conclusion
I expected to find more information in the doc or somewhere about which format is required for object’s storage. Regarding saveobj and loadobj I may have missed something and somebody might point out where to find answers. I’m looking for a way to save objects with the save command and I’d like to use saveobj and loadobj to make this possible.
Thanks,
Francois

My system

  • OS:Windows 10 (version 2004)
  • Octave version: Version 6.1.0

Saving and loading classdef objects is a feature that hasn’t been implemented yet.

The saveobj and loadobj functions only work with legacy class objects, not classdef.

Just for the files, this is bug GNU Octave - Bugs: bug #45833, support load/save of classdef... [Savannah].

Now it’s clear thank you very much for your support.
However I may not use the legacy class declaration style. Because with legacy class I have not found how to implement handle class. Moreover saving the links when one class is referencing another one is not obvious. In my model there are circular references and other difficulties to store objects. In my case I think a generic approach requires complex algorithms and I will probably customize the storage of my objects and their interdependencies.
Thanks for this great software!