Conditional properties in classdefs

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

回答(1 个)

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 个评论

Great, thanks a lot, that worked! I have a followup question. I have another class called ModelML. Can I define objects in ModelML using conditional statements based on the values of objects that I have defined in DataML? For instance:
classdef ModelML
properties
paramlist
methods
function inds = get.paramlist(nest_ID )
if nest_ID == [ 1 2 2 2 3 ]
inds = {'sigma'}
end
end
end
end
How can I call objects from DataML when I'm in ModelML?
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

请先登录,再进行评论。

类别

帮助中心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!

Translated by