Nested functions in a class - Call the function without including the class name as filename.function

2 visualizaciones (últimos 30 días)
Hello MATLAB experts,
I defined a class with different functions that are called from external .m files but also call eachother reciprocally, that is the functions call other functions in the same class. As far as I could understand, I always have to use the call form classname.function to call a function from this class. However, I would rather avoid it for the function that are all in the same class in the same file (similarly to local functions), to make the code clearer.
For example:
classdef small_test
properties
PropertyName
end
methods (Static)
function d = sum_scale_test(a,b)
c = a+b;
d = small_test.scale_test(c);
end
function b = scale_test(a)
b=10*a;
end
end
end
Is there a way to skip the long call:
small_test.scale_test
and just use
scale_test
?
Maybe with some access option?
Thank you for the help!

Respuesta aceptada

Mitch Lautigar
Mitch Lautigar el 1 de Jun. de 2022
Editada: per isakson el 3 de Jun. de 2022
You can put subfunctions/nested functions below the Class call (after the end for classdef) and therefore have local functions for the class to reference. This will look like the following:
classdef <namehere>
properties
%....
end
methods
function obj = dosomething(obj)
end
end
function function_callsub1
%do stuff here
end
  3 comentarios
Mitch Lautigar
Mitch Lautigar el 1 de Jun. de 2022
No. They are local functions in this case. If you want regular functions, you need to create the functions outside of the class.
fr td
fr td el 2 de Jun. de 2022
Editada: fr td el 2 de Jun. de 2022
Thank you. I had to separate the functions that are only used within the class file from those that are used externally and in one case I had to copy the function both inside the class (for external use) and below the class end for local use. It works fine.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Software Development Tools en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by