Info

This question is closed. Reopen it to edit or answer.

How to write this class- 'nn-dependent property should not access...' warning

1 view (last 30 days)
Arwel
Arwel on 7 Aug 2018
Closed: MATLAB Answer Bot on 20 Aug 2021
Hi,
I have a class which I define as follows....
classdef problemDef
properties
nParams
params
paramNames
paramConstr
end
methods
%Parameters block
function obj = set.nParams(obj,nParams)
if (isnan(nParams) || isinf(nParams) || mod(nParams,1) ~= 0)
error('nParams must be real integer');
end
obj.nParams = nParams;
if isempty(obj.params)
obj.params = zeros(1,nParams);
elseif (length(obj.params) < nParams)
obj = obj.extendNParams(nParams);
elseif (length(obj.params) > nParams)
obj = obj.contractNParams(nParams);
end
if isempty(obj.paramNames)
obj.paramNames = cell(1,nParams);
elseif (length(obj.paramNames) < nParams)
obj = obj.extendNParamNames(nParams);
elseif (length(obj.params) > nParams)
obj = obj.contractNParamNames(nParams);
end
if isempty(obj.paramConstr)
obj.paramConstr = cell(1,nParams);
elseif (length(obj.paramConstr) < nParams)
obj = obj.extendNParamConstr(nParams);
elseif (length(obj.paramConstr) > nParams)
obj = obj.contractNParamConstr(nParams);
end
end
function obj = set.params(obj,val)
if (length(val) > obj.nParams)
error('Length of values vector must be <= nparams');
elseif (isnumeric(val) == false | isinteger(val) == true)
error('Params must be vector of doubles');
else
obj.params = val;
end
end
function obj = set.paramNames(obj,val)
if (length(val) > obj.nParams)
error('Length of paramNames vector must be <= nparams');
elseif (iscell(val) == false)
error('paramNames must be cell array');
else
obj.paramNames = val;
end
end
function obj = set.paramConstr(obj,val)
if (length(val) > obj.nParams)
error('Length of poramConstr vector must be <= nParams');
elseif (iscell(val) == false)
error('paramConstr must be cell array');
else
for i = 1:length(val)
thisVal = val{i};
if isempty(thisVal)
val{i} = [0 0];
elseif (~isnumeric(thisVal) || (size(thisVal,1)~=1 || size(thisVal,2)~=2) || isinteger(val))
error('value %d in assignment to paramConstr must be 1x2 double',i);
end
end
end
obj.paramConstr = val;
end
%End parameters block
end
methods (Access = private)
function obj = extendNParams(obj,val)
currentLength = length(obj.params);
new = zeros(1,(val-currentLength));
new = [obj.params,new];
obj.params = new;
end
function obj = contractNParams(obj,val)
obj.params = obj.params(1:val);
end
function obj = extendNParamNames(obj,val)
currentLength = length(obj.paramNames);
new = cell([1,(val-currentLength)]);
new = horzcat([obj.paramNames],new);
obj.paramNames = new;
end
function obj = contractNParamNames(obj,val)
obj.params = obj.params{1:val};
end
function obj = extendNParamConstr(obj,val)
currentLength = length(obj.paramConstr);
new = cell([1,(val-currentLength)]);
new(:) = {[0 0]};
new = horzcat([obj.paramConstr],new);
obj.paramConstr = new;
end
function obj = contractNParamConstr(obj,val)
obj.paramConstr = obj.paramConstr(1:val);
end
end
end
This hows the behaviur I want. So I can make an 'empty' class....
>> a = problemDef
a =
problemDef with properties:
nParams: []
params: []
paramNames: []
paramConstr: []
>>
...then everything is controlled by the 'nParams' property. So, when I set nParams, the size of all the other arrays is set by this....
>> a.nParams = 5
a =
problemDef with properties:
nParams: 5
params: [0 0 0 0 0]
paramNames: {[] [] [] [] []}
paramConstr: {[0 0] [0 0] [0 0] [0 0] [0 0]}
>>
I can then set the values of the arrays as I like with normal Matlab indexing....
>> a.paramConstr{1} = [-inf inf]; a.params(3:end) = 10; a.paramNames{1} = 'HelloWorld'; a.paramConstr{1} = [-inf inf]
a =
problemDef with properties:
nParams: 5
params: [0 0 10 10 10]
paramNames: {'HelloWorld' [] [] [] []}
paramConstr: {[-Inf Inf] [0 0] [0 0] [0 0] [0 0]}
>>
This is exactly the behaviour I want, and seems to work. However, in the 'set.nParams' block, a lot of the calls are underlined with the warning 'A set method for a non-dependent property should not access anothe property..'.
I'm not used to working with classes, so I'm not totally sure what this means (have checked the documentation but still not sure). Can someone suggest how I should write the class to get the behaviour I have here, without triggering this warning? (Or even is this warning something I can ignore in this case??)
Cheers,
Arwel

Answers (0)

Community Treasure Hunt

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

Start Hunting!