Maybe it's a trivial question but I would like to know how to access a constant property within the class contructor or a class function in octave. Let's make an example:
classdef Example % < FatherClass
% read-only protected properties
properties (Constant=true, Access=protected)
all_levels = {"A","B","C","D"};
endproperties
% protected properties
properties(Access=protected)
level = 'D';
output = '.';
endproperties
methods(Access=public)
function obj = Example (level,outputfilepath)
if any(strcmp(all_levels,level))
obj.level = level;
else
error ("possible levels are: A B C D");
endif
obj.output = outputfilepath
endfunction
endmethods
end
running this class example I receive the error:
error: 'all_levels' undefined near line 12, column 12
error: called from
Example at line 12 column 13
So, I've tried something like
if any(strcmp(obj.all_levels,level))
obj.level = level;
With the same result, also defining a getter:
methods (Static = true)
function lvs = gel_levels()
lvs = all_levels
endfunction
endmethods
...
methods(Access=public)
function obj = Example (obj,level,outputfilepath)
all_levels = get_levels()
% disp(all_levels)
if any(strcmp(all_levels,level))
obj.level = level;
else
error ("possible levels are: A B C D");
endif
obj.output = outputfilepath
endfunction
endmethods
Sorry but I'm quite new to octave and I haven't found any example about this. What I'm trying to accomplish is a simple class variable
CodePudding user response:
The question is a bit confusing, as different attempts seem to be using different parts, but in general I think your problem is that you are not passing the object as a formal parameter in the method.
It is also not clear if you are trying to modify the object "in-place", or trying to generate a new one ... but in any case remember that modifying objects in place is not possible (unless inheriting from the 'handle' object). Therefore the typical thing you're supposed to do is: pass the object in as the first input as you're supposed to do with class method definitions, modify it, return it, and then when you're using this method in your calling workspace, capture this object (typically in a variable by the same name as the called object in the calling workspace) via assignment.
This works for me:
%% in Example.m
classdef Example
% read-only protected properties
properties( Constant=true, Access=protected )
all_levels = {"A", "B", "C", "D"};
endproperties
% protected properties
properties( Access = protected )
level = 'D';
output = '.';
endproperties
methods( Access = public )
function obj = Logging( obj, level, outputfilepath )
valid_level_choice = any( strcmp( obj.all_levels, level ) );
if valid_level_choice, obj.level = level;
else, error( "possible levels are: A B C D" );
endif
obj.output = outputfilepath;
endfunction
function get_level( obj )
fprintf( "The level is %s\n;", obj.level );
endfunction
endmethods
endclassdef
%% In your console session
E = Example();
E.get_level()
%> The level is D
E = E.Logging( 'A', './' );
E.get_level()
%> The level is A