OOP: passing arguments to a handle class method

조회 수: 4 (최근 30일)
Manish Makwana
Manish Makwana 2012년 8월 14일
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?

채택된 답변

per isakson
per isakson 2012년 8월 14일
편집: per isakson 2012년 8월 14일
I assume that
  1. function ErrorVal = HeightCorrection(tank)
  2. function Err = OutOfBoundsCorrection(obj)
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.
  댓글 수: 1
Manish Makwana
Manish Makwana 2012년 8월 15일
Thank you so much! This seems to have done the trick :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by