Class property validator reports an error if no default value is set
Show older comments
I have the following class defintion which is using a validator function and a custom constructor with an input parser.
classdef MyClass
properties
hWriteFunc(1,1) function_handle
end
methods
function obj = MyClass(varargin)
% Parse input vars
p = inputParser;
addParameter(p,"hWriteFunc",@defaultWriteFunc);
parse(p,varargin{:});
% Set properties
obj.hWriteFunc = p.Results.hWriteFunc;
end
end
end
function defaultWriteFunc(~)
end
When creating an instance of that class by executing
bar = MyClass();
I get the following error:
>> Error defining property 'hWriteFunc' of class 'MyClass'. Unable to construct default object of class function_handle.
Obviously, I can set a default property to avoid the error:
classdef MyClass
properties
hWriteFunc(1,1) function_handle = @defaultWriteFunc;
end
methods
function obj = MyClass(varargin)
% Parse input vars
p = inputParser;
addParameter(p,"hWriteFunc",@defaultWriteFunc);
parse(p,varargin{:});
% Set properties
obj.hWriteFunc = p.Results.hWriteFunc;
end
end
end
function defaultWriteFunc(~)
end
The problem is I now have two points in the class defintion where I need to set the default value (in the properties section and in the custom constructor). I would consider this a bad practise because I am defining the same value on two separate locations and would like to avoid this.
How am I able to define the default value at only one location while still using a validator and the input parser?
Accepted Answer
More Answers (3)
You can write your own validation function

classdef MyClass
properties
hWriteFunc(1,1) {mustBeFunctionHandle} = @defaultWriteFunc
end
methods
function obj = MyClass(varargin)
if nargin>0
% if user calls the constructor with a function handle
obj.hWriteFunc = varargin{1};
end
end
end
end
function mustBeFunctionHandle(x)
if ~isa(x,'function_handle')
error("Value of Data property must be fHandle")
end
end
3 Comments
Michael
on 17 Apr 2024

classdef MyClass
properties
hWriteFunc(1,1) {mustBeA(hWriteFunc,'function_handle')} = @defaultWriteFunc
end
methods
function obj = MyClass(varargin)
if nargin>0
% if user calls the constructor with a function handle
obj.hWriteFunc = varargin{1};
end
end
end
end
Michael
on 17 Apr 2024
classdef MyClass
properties
WriteFunc
end
methods
function obj = MyClass(varargin)
% Parse input vars
p = inputParser;
addParameter(p,"hWriteFunc",@defaultWriteFunc);
parse(p,varargin{:});
% Set properties
obj.hWriteFunc = p.Results.hWriteFunc;
end
function obj=set.WriteFunc(obj,val)
arguments
obj
val {mustBeA(val,["double","function_handle"])}
end
if isnumeric(val), assert( isempty(val) ,'Must be function handle or []') ;end
obj.WriteFunc=val;
end
end
end
I have the following class defintion which is using a validator function
You're not using a property validation function anywhere that I can see. Property validation functions start with "mustBe", e.g., mustBeReal.
You do have a converter to type "function_handle", which I don't think you really need, but if you really want it there, just define any old function handle for the property default, e.g.,
classdef MyClass
properties
hWriteFunc function_handle = @() []
end
end
Categories
Find more on Argument Definitions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!