Conditional properties in classdefs

조회 수: 25 (최근 30일)
Mihir Sharma
Mihir Sharma 2018년 10월 27일
댓글: per isakson 2018년 10월 27일
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개)

per isakson
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
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 CenterFile 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