OOP: passing arguments to a handle class method

3 ビュー (過去 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 Center および File ExchangePerformance and Memory についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by