'Octave: create a class variable then access it within class (in constructor)
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
