- function ErrorVal = HeightCorrection(tank)
- function Err = OutOfBoundsCorrection(obj)
OOP: passing arguments to a handle class method
4 次查看(过去 30 天)
显示 更早的评论
Hi,
I'm writing a method that takes a double data type (from a property of a local class object) and then does some calculations with it:
function Err = OutOfBoundsCorrection(obj)
% determine error to add to species for exceeding system limits
ErrorVal = zeros(2,2); % initialise array to hold error values
ErrorVal(1,1) = HeightCorrection(obj.SimTankA.Height); % error from tank A height being too low or high
ErrorVal(2,1) = TempCorrection(obj.SimTankA.Temp); % error from tank A temp being too low or high
ErrorVal(1,2) = HeightCorrection(obj.SimTankB.Height); % error from tank B height being too low or high
ErrorVal(2,2) = TempCorrection(obj.SimTankB.Temp); % error from tank B temp being too low or high
Err = sum(ErrorVal); % sum errors together
end
function ErrorVal = HeightCorrection(tank)
% determine error from tank height approaching or exceeding bounds
if (tank < 0.2) % fluid height is below 0.2 metres -> MIN 0
ErrorVal = 100 * 1/tank; % inverse of height by an arbitary penalty
else if (tank > 2.3) % fluid height is above 2.3 metres - > MAX 2.5
ErrorVal = 10000 * (tank - 2.3); % remove offset and multiply by arbitrary penalty
end
end
end
However i keep getting an error message from matlab: 'Undefined function 'HeightCorrection' for input arguments of type 'double'. Not sure what's causing this. Something to do with the way I'm calling the property, or writing the argument term in the method?
Has anyone got suggestions?
0 个评论
采纳的回答
per isakson
2012-8-14
编辑:per isakson
2012-8-14
I assume that
each defines a method (not static) of a class. If so the syntax of the call should be
ErrorVal(1,1) = HeightCorrection( obj, obj.SimTankA.Height );
or
ErrorVal(1,1) = obj.HeightCorrection(obj.SimTankA.Height);
and the signature of the method should be
function ErrorVal = HeightCorrection( obj, tank )
.
It is possible to keep the code as is and make, HeightCorrection, an ordinary separate function.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Performance and Memory 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!