In the following class definition file I am trying to create an object that can be conditionally saved using the SAVEOBJ method.
classdef myobj properties val = 1; end methods function obj = myobj(in) obj.val = in; end function obj = saveobj(obj) if obj.val~=1 error ('Save aborted') else disp('Saving object.'); end end endend
I try to create and save an object as follows:
a = myobj(2);save('savefile.mat','a')
But when the control reaches the saveobj method, my expected error is thrown as a warning:
Warning: Error saving an object of class 'myobj': Error using ==> myobj>myobj.saveobj at 15 Save aborted
and the MAT file is created anyway.
Best Answer