필터 지우기
필터 지우기

Error defining single class property in Enumeration Class

조회 수: 1 (최근 30일)
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!

채택된 답변

Michele Esposito Marzino
Michele Esposito Marzino 2022년 9월 12일
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

추가 답변 (1개)

chrisw23
chrisw23 2022년 9월 9일
편집: chrisw23 2022년 9월 9일
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"

카테고리

Help CenterFile Exchange에서 Structured Data and XML Documents에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by