Conditional properties in classdefs
25 次查看(过去 30 天)
显示 更早的评论
Hello,
I am new to MATLAB and object-oriented programming. I am working with a class which has the following property:
classdef DataML < ModelData
properties
nest_ID = [ 1 1 2 2 3 ]; % current model
% nest_ID = [1 2 2 2 3], % alternative model specification
% nest_ID = [1 1 1 2 3]) % alternative model specification
nest_index = [1,2,3,4]; % current model
nest_index = [2,3,4]; % alternative model specification
nest_index = [1,2,3]; % alternative model specification
end
end
I have to run my model for several values of nest_ID (e.g. %nest_ID = [1 2 2 2 3], nest_ID = [1 1 1 2 3]). nest_index is a variable that depends on nest_ID and hence needs to be defined conditionally depending on the value of nest_ID. I want to make my class more flexible: depending on the value of the nest_ID array, I want nest_index to take different values. I tried the following code with if else conditions but I get a syntax error saying that I cannot include conditional statements in class properties.
classdef DataML < ModelData
properties
nest_ID = [ 1 1 2 2 3 ]; % current model
% nest_ID = [1 2 2 2 3], % alternative model specification
% nest_ID = [1 1 1 2 3]) % alternative model specification
if nest_ID = [ 1 1 2 2 3 ]
nest_index = [1,2,3,4]; % current model
elseif if nest_ID = [1 2 2 2 3] % alternative model specification
nest_index = [2,3,4];
elseif if nest_ID = [1 1 1 2 3] % alternative model specification
nest_index = [1,2,3];
end
end
end
Can you please advice how I can do this?
Many thanks, Mihir
0 个评论
回答(1 个)
per isakson
2018-10-27
Try put the calculations in the constructor and provide the model ID as input
classdef DataML < ModelData
properties
nest_ID
nest_index
end
methods
function this = DataML( id )
if all( id == [ 1 1 2 2 3 ] )
this.nest_index = [1,2,3,4];
elseif all( id == [1 2 2 2 3] )
this.nest_index = [2,3,4];
elseif all( id == [1 1 1 2 3] )
this.nest_index = [1,2,3];
else
error('Unknown input value')
end
this.nest_ID = id;
end
end
end
3 个评论
per isakson
2018-10-27
Try
>> mml = ModelML
mml =
ModelML with properties:
paramlist: [1 2 3 4 5 6]
>> mml.get_paramlist([1,1,1,2,3])
1 2 3
ans =
1 2 3 4 5 6
where
classdef DataML < handle % ModelData
properties
nest_ID
nest_index
end
methods
function this = DataML( id )
if all( id == [ 1 1 2 2 3 ] )
this.nest_index = [1,2,3,4];
elseif all( id == [1 2 2 2 3] )
this.nest_index = [2,3,4];
elseif all( id == [1 1 1 2 3] )
this.nest_index = [1,2,3];
else
error('Unknown input value')
end
this.nest_ID = id;
end
end
end
and
classdef ModelML
properties
paramlist = [1,2,3,4,5,6];
end
methods
function inds = get_paramlist( this, nest_ID )
if all( nest_ID == [ 1 2 2 2 3 ] )
inds = {'sigma'};
else
inds = this.paramlist;
end
data_model = DataML( nest_ID );
disp( data_model.nest_index )
end
end
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Construct and Work with Object Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!