Conditional properties in classdefs

25 visualizaciones (últimos 30 días)
Mihir Sharma
Mihir Sharma el 27 de Oct. de 2018
Comentada: per isakson el 27 de Oct. de 2018
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

Respuestas (1)

per isakson
per isakson el 27 de Oct. de 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 comentarios
per isakson
per isakson el 27 de Oct. de 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

Iniciar sesión para comentar.

Categorías

Más información sobre Construct and Work with Object Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by