Is there a built-in function to validate a class property that should be a cell array?
Show older comments
I'm new to classes and learning about how to define properties and validate their values.
I have a class with a property A that is a cell array of doubles (possibly matrices). For example:
A =
1×2 cell array
{[0.7000]} {[0.9000]}
classdef MKFObserver < matlab.mixin.Copyable
properties (SetAccess = immutable)
Ts {mustBeNumeric}
n {mustBeInteger}
%...etc
end
properties
A % how to validate this?
B
C
D
P {mustBeNumeric}
Q {mustBeNumeric}
R {mustBeNumeric}
%...etc
end
Can I use a built in function like {mustBeNumeric} or do I need to write my own validation function?
3 Comments
per isakson
on 4 Jun 2022
Bill Tubbs
on 4 Jun 2022
Edited: Bill Tubbs
on 4 Jun 2022
per isakson
on 4 Jun 2022
Something like this
A (1,2) {mustBeUnderlyingType(A,'cell')}
Not tested
Accepted Answer
More Answers (2)
There is always iscell():
classdef
...
methods
function obj=set.A(obj,A)
assert(iscell(A),'Value assigned to A must be cell array');
obj.A=A;
end
end
end
Sean de Wolski
on 5 Jun 2022
Edited: Sean de Wolski
on 5 Jun 2022
I think you'll have to write your own. Must be underlying type is for distributed, gpuArrays and others that masquerade as the underlying type.
mustBeUnderlyingType({pi},'double')
You could probably get away with this as your validator: cellfun(@(x)mustBeA(x, 'double'),PropertyName)
c = {pi,magic(3)}
d = {pi, "hello"}
cellfun(@(x)mustBeA(x, 'double'),c)
cellfun(@(x)mustBeA(x, 'double'),d)
Categories
Find more on Properties 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!