Conditional properties in classdefs
25 views (last 30 days)
Show older comments
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 Comments
Answers (1)
per isakson
on 27 Oct 2018
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 Comments
per isakson
on 27 Oct 2018
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
See Also
Categories
Find more on Construct and Work with Object Arrays 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!