Is there a built-in function to validate a class property that should be a cell array?

17 Ansichten (letzte 30 Tage)
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 Kommentare
Bill Tubbs
Bill Tubbs am 4 Jun. 2022
Bearbeitet: Bill Tubbs am 4 Jun. 2022
Thanks. That will do the first part. But I'll have to upgrade my MATLAB now! (Introduced in R2020b). I admit I have been putting it off for too long...

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Edric Ellis
Edric Ellis am 6 Jun. 2022
Rather than a validator, you could perhaps just use the class specification? (In my code below I'm using a function with an arguments block, but the syntax is exactly the same as a properties block in a classdef). Note that this will accept anything that can be converted to a cell, if you care about distributed arrays, then you should use the approach suggested by @Sean de Wolski.
myFcn({1,2}); % works
myFcn([1,2]); % fails
Error using solution>myFcn
Invalid argument at position 1. Value must be of type cell or be convertible to cell.
function myFcn(in)
arguments
in (1,2) cell
end
% do stuff
end
  1 Kommentar
Bill Tubbs
Bill Tubbs am 6 Jun. 2022
Bearbeitet: Bill Tubbs am 6 Jun. 2022
This worked! I just did this:
classdef MKFObserver < matlab.mixin.Copyable
properties
A cell
B cell
%...etc
% Test:
>> MKFObserver([0.7 0.9], ...)
Error setting property 'A' of class 'MKFObserver':
Invalid data type. Value must be cell or be convertible to cell
It doesn't check the underlying type of the elements of the cell array but I decided in the end I don't want this level of validation. Thanks.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Matt J
Matt J am 4 Jun. 2022
Bearbeitet: Matt J am 4 Jun. 2022
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
Sean de Wolski am 5 Jun. 2022
Bearbeitet: Sean de Wolski am 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')
Value must have underlying type '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)

Kategorien

Mehr zu Construct and Work with Object Arrays finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by