OOP: passing arguments to a handle class method

4 visualizaciones (últimos 30 días)
Manish Makwana
Manish Makwana el 14 de Ag. de 2012
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?

Respuesta aceptada

per isakson
per isakson el 14 de Ag. de 2012
Editada: per isakson el 14 de Ag. de 2012
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 comentario
Manish Makwana
Manish Makwana el 15 de Ag. de 2012
Thank you so much! This seems to have done the trick :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Performance and Memory en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by