Clear Filters
Clear Filters

Error defining single class property in Enumeration Class

10 views (last 30 days)
Hello everyone, I am struggling to find how to make this work in MATLAB.
I have an Enumeration Class, which has its enumerators plus a single class property which is a function pointer to some (same class) Static Functions. In other words, depending on the enumerator value, the function pointer will point to its related function, so that I can then have an ```evaluate()``` function which calls the function pointer under the hood.
Here is the idea:
classdef PSDType < handle
enumeration
DAVENPORT (PSDType.DavenportPSD)
VON_KARMAN (PSDType.VonKarmanPSD)
KAIMAL (PSDType.KaimalPSD)
EUROCODE (PSDType.EurocodePSD)
ORNSTEIN_UHLENBECK (PSDType.OU_PSD)
end
properties (Access = private)
func_ptr function_handle
end
methods (Access = public)
function S = evaluate(this, varargin)
try
S = this.func_ptr(varargin{:});
catch ex
warning('\n%s', ...
'@PSDType.evaluate: exception evaluating PSD');
getReport(ex);
end
end
end
methods (Static = true)
... here static methods to point to
end
end
However, once I run, as soon as it load into memory the Class which has as one of its member functions this Enumeration Class instance, I get this error
Invalid default value for property 'PSD_TYPE' in class 'WindData':
Not enough input arguments.
memb PSDType = PSDType.DAVENPORT % (default initialisation in class member instance)
I even added the Class constructor:
function this = PSDType(f_ptr)
this.func_ptr = f_ptr;
end
with no changes.
What do I do wrong?
Thanks a lot for your answers!

Accepted Answer

Michele Esposito Marzino
Michele Esposito Marzino on 12 Sep 2022
I finally found I was missing the "@" operator when assigning function to point to. @chrisw23 thanks for yor answer, I would bet it works, but I wanted to avoid the switch statement.
This worked fine finally:
classdef PSDType < handle
enumeration
DAVENPORT (@PSDType.DavenportPSD)
VON_KARMAN (@PSDType.VonKarmanPSD)
KAIMAL (@PSDType.KaimalPSD)
EUROCODE (@PSDType.EurocodePSD)
ORNSTEIN_UHLENBECK (@PSDType.OU_PSD)
end
properties (Access = private)
psd_func_ptr function_handle
end
methods (Access = public)
function this = PSDType(f_ptr)
this.psd_func_ptr = f_ptr;
end
function PSDeval = evaluate(this, varargin)
...
PSDeval = this.psd_func_ptr(varargin{:});
end % evaluate()
end
end

More Answers (1)

chrisw23
chrisw23 on 9 Sep 2022
Edited: chrisw23 on 9 Sep 2022
classdef PSDType
enumeration
DAVENPORT
VON_KARMAN
KAIMAL
EUROCODE
ORNSTEIN_UHLENBECK
end
methods (Access = public)
function obj = PSDType()
% constructor
end
function s = evaluate(obj, varargin)
s = [];
switch obj
case PSDType.DAVENPORT
s = obj.callDavenPort(varargin);
case PSDType.VON_KARMAN
case PSDType.KAIMAL
case PSDType.EUROCODE
case PSDType.ORNSTEIN_UHLENBECK
end
end
end
methods (Access = 'private')
function s = callDavenPort(obj,varargin)
s = "evalDavenPort";
end
end
end
How about this ?
PSDType.DAVENPORT.evaluate
"evalDavenPort"

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!